Compare commits

...

1 Commits

Author SHA1 Message Date
ff4cc47e60 Unknown 2023-01-29 07:50:58 -05:00
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 {Config} from '/scripts/lib/data-file';
import {Logger} from '/scripts/lib/logger';
import {copyWithDependencies} from '/scripts/copy';
const configPath = '/etc/botnet.txt';
const port = 1;
class Manager {
running;
workers = [];
constructor(ns, device, port, config = '/conf/botnet.txt') {
async constructor(ns, hostname) {
ns.disableLog('ALL');
this.ns = ns;
this.config = config;
this.device = device;
this.config = new Config(configPath);
this.hostname = hostname;
this.logger = new Logger(this.ns, [
() => `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`
@ -124,7 +128,8 @@ class Manager {
export async function main(ns) {
// Setup
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.', [
new ArgParser('copy', 'Copy file & dependencies to botnet', [
{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', [
{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', [
{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},
@ -154,9 +160,9 @@ export async function main(ns) {
// Run command
if(args['_command'] == 'start') { // Start botnet manager
ns.tprint(`Starting ${hostname} as botnet manager`);
ns.tprint(`Connect more nodes with: run botnet-manager.js join [SERVER]`);
await new Manager(ns, hostname, portNum).start();
ns.tprint(`Starting botnet controller on: ${hostname}`);
ns.tprint(`Connect workers to botnet with: run botnet-manager.js join [SERVER]`);
await new Manager(ns, hostname).start();
} else if(args['_command'] == 'copy') { // Issue copy command
await ns.writePort(portNum, JSON.stringify({
command: 'copy',
@ -176,6 +182,9 @@ export async function main(ns) {
command: 'leave',
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
await ns.writePort(portNum, JSON.stringify({
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]; }
}