Added find-target & other updates

This commit is contained in:
Zakary Timson 2022-03-19 13:55:27 -04:00
parent 436a9f278e
commit 88ff84c082
6 changed files with 157 additions and 63 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
.vscode

View File

@ -5,10 +5,12 @@ These scripts are for playing the [open source](https://github.com/danielyxie/bi
- [BitBurner - Scripts](#bitburner-scripts) - [BitBurner - Scripts](#bitburner-scripts)
- [Table of Contents](#table-of-contents) - [Table of Contents](#table-of-contents)
- [Quick Start](#quick-start) - [Quick Start](#quick-start)
- [Scripts](#scripts) - [Scripts](#scripts)
- [botnet-manager.js (WIP)](#botnetmanagerjswip)
- [connect.js](#connectjs) - [connect.js](#connectjs)
- [copy.js](#copyjs) - [copy.js](#copyjs)
- [crawler.js](#crawlerjs) - [crawler.js](#crawlerjs)
- [find-target.js](#besttargetjs)
- [hacknet-manager.js](#hacknet-managerjs) - [hacknet-manager.js](#hacknet-managerjs)
- [miner.js](#minerjs) - [miner.js](#minerjs)
- [network-graph.js](#network-graphjs) - [network-graph.js](#network-graphjs)
@ -42,6 +44,14 @@ Learn more about the [availible scripts](#scripts) bellow or pass the `--help` f
## Scripts ## Scripts
### [botnet-manager.js (WIP)](./scripts/botnet-manager.js)
**RAM:** GB
Connect & manage a network of devices to launch distributed attacks.
```
```
### [connect.js](./scripts/connect.js) ### [connect.js](./scripts/connect.js)
**RAM:** 1.85 GB **RAM:** 1.85 GB
@ -111,6 +121,28 @@ Options:
-h --help Display this help message -h --help Display this help message
``` ```
### [find-target.js](./scripts/find-target.js)
**RAM:** 6.00 GB
Scan the network for the best device(s) to mine.
```
[home ~/]> run scripts/find-target.js --help
Running script with 1 thread(s), pid 1 and args: ["--help"].
/scripts/find-target.js:
Scan the network for the best device(s) to mine.
Usage: run find-target.js [OPTIONS]
run find-target.js --help
Options:
-c --count Number of devices to return in order from best to worst
-r --rooted Filter to devices that have been rooted
-n --not-rooted Filter to devices that have not been rooted
-v --verbose Display the estimated income per minute per core
-h --help Display this help message
```
### [hacknet-manager.js](./scripts/hacknet-manager.js) ### [hacknet-manager.js](./scripts/hacknet-manager.js)
**RAM:** 5.70 GB **RAM:** 5.70 GB

View File

@ -6,7 +6,7 @@ class Manager {
running; running;
workers = []; workers = [];
constructor(ns, device, port, config = '/conf/swarm.txt') { constructor(ns, device, port, config = '/conf/botnet.txt') {
this.ns = ns; this.ns = ns;
this.config = config; this.config = config;
this.device = device; this.device = device;
@ -108,7 +108,7 @@ class Manager {
export async function main(ns) { export async function main(ns) {
// Setup // Setup
ns.disableLog('ALL'); ns.disableLog('ALL');
const argParser = new ArgParser('swarm.js', 'Manage a swarm of devices.', [ const argParser = new ArgParser('botnet-manager.js', 'Connect & manage a network of devices to launch distributed attacks.', [
'COPY [--HELP] [OPTIONS] FILE [DEST]', 'COPY [--HELP] [OPTIONS] FILE [DEST]',
'JOIN [--HELP] MANAGER [DEVICE]', 'JOIN [--HELP] MANAGER [DEVICE]',
'KILL [--HELP]', 'KILL [--HELP]',

42
scripts/find-target.js Normal file
View File

@ -0,0 +1,42 @@
import {ArgError, ArgParser} from '/scripts/lib/arg-parser';
import {bestTarget} from '/scripts/lib/utils';
/**
* Scan the network for the best device(s) to mine.
* @param ns {NS} - BitBurner API
* @returns {*}
*/
export function main(ns) {
// Setup
ns.disableLog('ALL');
const argParser = new ArgParser('find-target.js', 'Scan the network for the best device(s) to mine.', null, [
{name: 'count', desc: 'Number of devices to return in order from best to worst', flags: ['-c', '--count'], default: Infinity, type: 'number'},
{name: 'rooted', desc: 'Filter to devices that have been rooted', flags: ['-r', '--rooted'], type: 'bool'},
{name: 'notRooted', desc: 'Filter to devices that have not been rooted', flags: ['-n', '--not-rooted'], type: 'bool'},
{name: 'verbose', desc: 'Display the estimated income per minute per core', flags: ['-v', '--verbose'], type: 'bool'},
]);
try {
// Run
const args = argParser.parse(ns.args);
// Banner
ns.tprint('===================================================');
ns.tprint(`Finding Targets`);
ns.tprint('===================================================');
// Search & display results
bestTarget(ns).filter((t, i) => !args['count'] || i < args['count'])
.filter(t => !args['rooted'] || t.hasAdminRights)
.filter(t => !args['notRooted'] || !t.hasAdminRights)
.map(t => `${t.hostname}${args['verbose'] ? ` (${t.moneyAMinute.toLocaleString('en-US', {
style: 'currency',
currency: 'USD',
})})` : ''}`)
.forEach((t, i) => ns.tprint(`${i + 1}) ${t}`));
ns.tprint('');
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
}

View File

@ -1,3 +1,20 @@
/**
* Scan the entire network for the best device to hack.
* @param ns {NS} - BitBurner API
* @returns {string[]} - Sorted list of targets to hack based on financial return
*/
export function bestTarget(ns) {
const [devices, network] = scanNetwork(ns, 'home');
return devices.map(d => ns.getServer(d)).filter(d => d.hasAdminRights).map(d => ({
...d,
moneyAMinute: (ns.hackAnalyze(d.hostname) * ns.getServerMaxMoney(d.hostname)) * ((60 / (ns.getHackTime(d.hostname) / 1000)) * ns.hackAnalyzeChance(d.hostname))}
)).sort((a, b) => {
if(a.moneyAMinute < b.moneyAMinute) return 1;
if(a.moneyAMinute > b.moneyAMinute) return -1;
return 0;
});
}
/** /**
* Copy a file & scan it for dependencies copying them as well. * Copy a file & scan it for dependencies copying them as well.
* @param ns {NS} - BitBurner API * @param ns {NS} - BitBurner API

View File

@ -1,20 +1,22 @@
export class ArgError extends Error {} export class ArgError extends Error {}
export class ArgParser { class ArgParser {
/** /**
* Create a unix-like argument parser to extract flags from the argument list. Can also create help messages. * Create a unix-like argument parser to extract flags from the argument list. Can also create help messages.
* @param name {string} - Script name * @param name {string} - Script name
* @param desc {string} - Help text desciption * @param desc {string} - Help text desciption
* @param examples {string[]} - Help text examples * @param examples {string[]} - Help text examples
* @param argList {name: string, desc: string, flags: string[], type: string, default: any}[] - Array of CLI arguments * @param argList {name: string, desc: string, flags: string[], type: string, default: any}[] - Array of CLI arguments
* @param allowUnknown {boolean} - Allow unknown flags
*/ */
constructor(name, desc, examples, argList) { constructor(name, desc, examples, argList, allowUnknown = false) {
this.name = name ?? 'example.js'; this.name = name ?? 'example.js';
this.description = desc ?? 'Example description'; this.description = desc ?? 'Example description';
this.examples = examples || [`${argList.find(arg => !!arg.flags) ? '[OPTIONS] ' : ''}${argList.filter(arg => !arg.flags).map(arg => (arg.optional ? `[${arg.name.toUpperCase()}]` : arg.name.toUpperCase()) + (arg.extras ? '...' : '')).join(' ')}`]; this.examples = examples || [`${argList.find(arg => !!arg.flags) ? '[OPTIONS] ' : ''}${argList.filter(arg => !arg.flags).map(arg => (arg.optional ? `[${arg.name.toUpperCase()}]` : arg.name.toUpperCase()) + (arg.extras ? '...' : '')).join(' ')}`];
this.examples.push('--help'); this.examples.push('--help');
this.argList = argList || []; this.argList = argList || [];
this.argList.push({name: 'help', desc: 'Display this help message', flags: ['-h', '--help'], type: 'bool'}); this.argList.push({name: 'help', desc: 'Display this help message', flags: ['-h', '--help'], type: 'bool'});
this.allowUnknown = allowUnknown;
} }
/** /**
@ -38,7 +40,11 @@ export class ArgParser {
// Find & add flag // Find & add flag
const split = parse.split('='); const split = parse.split('=');
const arg = this.argList.find(arg => arg.flags && arg.flags.includes(split[0] || parse)); const arg = this.argList.find(arg => arg.flags && arg.flags.includes(split[0] || parse));
if(arg == null) throw new ArgError(`Option unknown: ${parse}`); if(arg == null) {
if(!this.allowUnknown) throw new ArgError(`Option unknown: ${parse}`);
extra.push(parse);
continue;
}
if(arg.name == 'help') throw new ArgError('Help'); if(arg.name == 'help') throw new ArgError('Help');
const value = arg.type == 'bool' ? true : split[1] || queue.splice(queue.findIndex(q => q[0] != '-'), 1)[0]; const value = arg.type == 'bool' ? true : split[1] || queue.splice(queue.findIndex(q => q[0] != '-'), 1)[0];
if(value == null) throw new ArgError(`Option missing value: ${arg.name}`); if(value == null) throw new ArgError(`Option missing value: ${arg.name}`);
@ -93,7 +99,7 @@ export class ArgParser {
* @params ns {NS} - Bitburner API * @params ns {NS} - Bitburner API
* @params file - Filename to display with progress bar * @params file - Filename to display with progress bar
*/ */
export async function downloadPrint(ns, file) { async function downloadPrint(ns, file) {
const speed = ~~(Math.random() * 100) / 10; const speed = ~~(Math.random() * 100) / 10;
const spacing = Array((40 - file.length) || 1).fill(' ').join(''); const spacing = Array((40 - file.length) || 1).fill(' ').join('');
await slowPrint(ns, `${file}${spacing}[==================>] 100%\t(${speed} MB/s)`); await slowPrint(ns, `${file}${spacing}[==================>] 100%\t(${speed} MB/s)`);
@ -101,12 +107,12 @@ export async function downloadPrint(ns, file) {
/** /**
* Print text to the terminal & then delay for a random amount of time to emulate execution time. * Print text to the terminal & then delay for a random amount of time to emulate execution time.
* @params ns {NS} - Bitburner API * @param ns {NS} - BitBurner API
* @params message {string} - Text to display * @param message {string} - Text to display
* @params min {number} - minimum amount of time to wait after printing text * @param min {number} - minimum amount of time to wait after printing text
* @params max {number} - maximum amount of time to wait after printing text * @param max {number} - maximum amount of time to wait after printing text
*/ */
export async function slowPrint(ns, message, min = 0.5, max = 1.5) { async function slowPrint(ns, message, min = 0.5, max = 1.5) {
const time = ~~(Math.random() * (max * 1000 - min * 1000)) + min * 1000; const time = ~~(Math.random() * (max * 1000 - min * 1000)) + min * 1000;
ns.tprint(message); ns.tprint(message);
await ns.sleep(time); await ns.sleep(time);
@ -125,58 +131,56 @@ export async function main(ns) {
{name: 'skip-self', desc: 'Skip updating self (for debugging & used internally)', flags: ['--skip-self'], type: 'bool'}, {name: 'skip-self', desc: 'Skip updating self (for debugging & used internally)', flags: ['--skip-self'], type: 'bool'},
{name: 'no-banner', desc: 'Hide the banner (Used internally)', flags: ['--no-banner'], type: 'bool'} {name: 'no-banner', desc: 'Hide the banner (Used internally)', flags: ['--no-banner'], type: 'bool'}
]); ]);
const src = 'https://gitlab.zakscode.com/ztimson/BitBurner/-/raw/develop/scripts/';
const dest = '/scripts/';
const fileList = [
'lib/arg-parser.js',
'lib/logger.js',
'lib/utils.js',
'connect.js',
'copy.js',
'crawler.js',
'hacknet-manager.js',
'miner.js',
'network-graph.js',
'rootkit.js'
];
let args;
try { try {
args = argParser.parse(ns.args || []); const args = argParser.parse(ns.args || []);
const src = 'https://gitlab.zakscode.com/ztimson/BitBurner/-/raw/develop/scripts/';
const dest = '/scripts/';
const fileList = [
'lib/arg-parser.js',
'lib/logger.js',
'lib/utils.js',
'botnet-manager.js',
'connect.js',
'copy.js',
'crawler.js',
'find-target.js',
'hacknet-manager.js',
'miner.js',
'network-graph.js',
'rootkit.js'
];
if(!args['no-banner']) {
// Banner
ns.tprint('===================================================');
ns.tprint(`Updating: ${args['device']}`);
ns.tprint('===================================================');
}
// Run
if(!args['skip-self']) { // Update self & restart
await slowPrint(ns, 'Updating self:');
await ns.wget(`${src}${updateFile}`, `${dest}${updateFile}`, args['device']);
await downloadPrint(ns, `${dest}${updateFile}`);
ns.tprint('');
await slowPrint(ns, 'Restarting...');
const pid = ns.run(`${dest}${updateFile}`, 1, args['device'], '--skip-self', '--no-banner');
if(pid == 0) ns.tprint('Failed');
else ns.tprint('Complete');
return await slowPrint(ns, '');
} else { // Update everything else
await slowPrint(ns, 'Downloading scripts:');
for(let file of fileList) {
await ns.wget(`${src}${file}`, `${dest}${file}`, args['device']);
await downloadPrint(ns, `${dest}${file}`);
}
ns.tprint('');
ns.tprint('Done!');
ns.tprint('');
}
} catch(err) { } catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message)); if(err instanceof ArgParser.ArgError) return ns.tprint(argParser.help(err.message));
throw err; throw err;
} }
if(!args['no-banner']) {
// Banner
ns.tprint('===================================================');
ns.tprint(`Updating: ${args['device']}`);
ns.tprint('===================================================');
}
// Run
if(!args['skip-self']) { // Update self & restart
await slowPrint(ns, 'Updating self:');
await ns.wget(`${src}${updateFile}`, `${dest}${updateFile}`, args['device']);
await downloadPrint(ns, `${dest}${updateFile}`);
ns.tprint('');
await slowPrint(ns, 'Restarting...');
const pid = ns.run(`${dest}${updateFile}`, 1, args['device'], '--skip-self', '--no-banner');
if(pid == 0) ns.tprint('Failed');
else ns.tprint('Complete');
return await slowPrint(ns, '');
} else { // Update everything else
await slowPrint(ns, 'Downloading scripts:');
for(let file of fileList) {
await ns.wget(`${src}${file}`, `${dest}${file}`, args['device']);
await downloadPrint(ns, `${dest}${file}`);
}
ns.tprint('');
ns.tprint('Done!');
ns.tprint('');
}
}
export function autocomplete(data) {
return [...data.servers];
} }