This commit is contained in:
Zakary Timson 2023-01-29 07:50:58 -05:00
parent c386cc7391
commit ff4cc47e60
9 changed files with 473 additions and 357 deletions

34
scripts/bitburner.js Normal file
View File

@ -0,0 +1,34 @@
/**
* Automatically complete the current BitNode.
*
* @param {NS} ns - BitBurner API
*/
export function main(ns) {
let modules = [
'auto-root',
'auto-hack',
'botnet-manager',
'hacknet-manager',
'server-manager'
];
// Banner
ns.run('/scripts/banner.js', 1, '-r');
ns.tprint(`Starting BitBurner with ${modules.length} enabled: `);
ns.tprint(modules.join(', '));
// botnet-manager
// hacknet-manager
ns.run('/scripts/hacknet-manager', 1, '-a');
// server-manager
ns.run('/scripts/server-manager', 1, '');
while(true) {
// auto-hack
ns.sleep(1000);
}
}

View File

@ -1,16 +1,20 @@
import {ArgParser} from '/scripts/lib/arg-parser'; import {ArgParser} from '/scripts/lib/arg-parser';
import {Config} from '/scripts/lib/data-file';
import {Logger} from '/scripts/lib/logger'; import {Logger} from '/scripts/lib/logger';
import {copyWithDependencies} from '/scripts/copy'; import {copyWithDependencies} from '/scripts/copy';
const configPath = '/etc/botnet.txt';
const port = 1;
class Manager { class Manager {
running; running;
workers = []; workers = [];
constructor(ns, device, port, config = '/conf/botnet.txt') { async constructor(ns, hostname) {
ns.disableLog('ALL'); ns.disableLog('ALL');
this.ns = ns; this.ns = ns;
this.config = config; this.config = new Config(configPath);
this.device = device; this.hostname = hostname;
this.logger = new Logger(this.ns, [ this.logger = new Logger(this.ns, [
() => `Botnet: ${device}`, () => `Botnet: ${device}`,
() => `Workers: ${this.workers.length}\tCores: ${this.workers.reduce((acc, w) => acc + w.cpuCores, 0)}\tRAM: ${this.workers.reduce((acc, w) => acc + w.maxRam, 0)} GB` () => `Workers: ${this.workers.length}\tCores: ${this.workers.reduce((acc, w) => acc + w.cpuCores, 0)}\tRAM: ${this.workers.reduce((acc, w) => acc + w.maxRam, 0)} GB`
@ -124,7 +128,8 @@ class Manager {
export async function main(ns) { export async function main(ns) {
// Setup // Setup
ns.disableLog('ALL'); ns.disableLog('ALL');
const hostname = ns.getHostname(), portNum = 1; const config = await new Config(ns, configPath).load();
const hostname = ns.getHostname();
const argParser = new ArgParser('botnet-manager.js', 'Connect & manage a network of servers to launch distributed attacks.', [ const argParser = new ArgParser('botnet-manager.js', 'Connect & manage a network of servers to launch distributed attacks.', [
new ArgParser('copy', 'Copy file & dependencies to botnet', [ new ArgParser('copy', 'Copy file & dependencies to botnet', [
{name: 'file', desc: 'File to copy', default: false}, {name: 'file', desc: 'File to copy', default: false},
@ -139,6 +144,7 @@ export async function main(ns) {
new ArgParser('leave', 'Disconnect worker node from swarm', [ new ArgParser('leave', 'Disconnect worker node from swarm', [
{name: 'device', desc: 'Device to disconnect, defaults to the current machine', optional: true, default: hostname} {name: 'device', desc: 'Device to disconnect, defaults to the current machine', optional: true, default: hostname}
]), ]),
new ArgParser('list', 'List connected worker nodes'),
new ArgParser('run', 'Copy & run script on the botnet', [ new ArgParser('run', 'Copy & run script on the botnet', [
{name: 'script', desc: 'Script to copy & execute', type: 'string'}, {name: 'script', desc: 'Script to copy & execute', type: 'string'},
{name: 'args', desc: 'Arguments for script. Forward the current target with: {{TARGET}}', optional: true, extras: true}, {name: 'args', desc: 'Arguments for script. Forward the current target with: {{TARGET}}', optional: true, extras: true},
@ -154,9 +160,9 @@ export async function main(ns) {
// Run command // Run command
if(args['_command'] == 'start') { // Start botnet manager if(args['_command'] == 'start') { // Start botnet manager
ns.tprint(`Starting ${hostname} as botnet manager`); ns.tprint(`Starting botnet controller on: ${hostname}`);
ns.tprint(`Connect more nodes with: run botnet-manager.js join [SERVER]`); ns.tprint(`Connect workers to botnet with: run botnet-manager.js join [SERVER]`);
await new Manager(ns, hostname, portNum).start(); await new Manager(ns, hostname).start();
} else if(args['_command'] == 'copy') { // Issue copy command } else if(args['_command'] == 'copy') { // Issue copy command
await ns.writePort(portNum, JSON.stringify({ await ns.writePort(portNum, JSON.stringify({
command: 'copy', command: 'copy',
@ -176,6 +182,9 @@ export async function main(ns) {
command: 'leave', command: 'leave',
value: args['device'] value: args['device']
})); }));
} else if(args['_command'] == 'list') {
ns.tprint('Botnet workers:');
ns.tprint(config['workers'].map(worker => worker.hostname).join(', '));
} else if(args['_command'] == 'run') { // Issue run command } else if(args['_command'] == 'run') { // Issue run command
await ns.writePort(portNum, JSON.stringify({ await ns.writePort(portNum, JSON.stringify({
command: 'run', command: 'run',

31
scripts/lib/data-file.js Normal file
View File

@ -0,0 +1,31 @@
export class DataFile {
/**
* Read & write data to a JSON file.
*
* @param {NS} ns - Bitburner API
* @param path - Path to config file
*/
constructor(ns, path) {
this.ns = ns;
this.path = path;
}
/**
* Load data file
*
* @returns {Promise<any>} - Saved data
*/
async load() {
return JSON.parse(await this.ns.read(this.path) || 'null');
}
/**
* Save data to file
*
* @param values - Data to save
* @returns {Promise<void>} - Save complete
*/
async save(values) {
await this.ns.write(this.path, JSON.stringify(values), 'w');
}
}

View File

@ -0,0 +1,42 @@
export class PortHelper {
/**
*
* @param ns
* @param port
* @param host
*/
constructor(ns, port, host) {
this.ns = ns;
this.host = host;
this.portNum = port;
this.port = ns.getPortHandle(port);
this.callbacks = {};
}
check() {
const pending = [];
while(!this.port.empty()) pending.push(this.port.read());
pending.filter(p => {
try {
const payload = JSON.parse(p);
if(this.callbacks[payload.subject]) return !this.callbacks[payload.subject](payload.value);
if(this.callbacks['*']) return !this.callbacks['*'](payload.value);
return true;
} catch {
return true;
}
}).forEach(p => this.port.write(p));
}
subscribe(subject, callback) { if(typeof callback == 'function') this.callbacks[subject] = callback; }
send(subject, value) {
this.ns.writePort(this.portNum, JSON.stringify({
from: this.host,
subject,
value
}));
}
unsubscribe(subject) { delete this.callbacks[subject]; }
}