add missing "nop.js". killall.js: rename to kill.js. add `--script` and `--server` options.
[?]
Apr 27, 2020, 5:29 PM
DJDWBCCQHQRKHGRKKQ32QY3WDEM7EKDDQ2YQLSSCS7HZTLHDRCFQCDependencies
- [2]
Y5OWMCQRall: remove unecessary uses of `exec`. refactor. main.js: add RAM reservation logic. lib_ram.js: renamed to lib_ram_server.js. README.js: update. - [3]
4ACCU75Nimprove and refactor scheduling logic. - [4]
3NFCZ6IPfixed the ram utilisation logic. added flags to `main.js` that can prevent the execution of helpers. - [5]
2BKHJI2Sinit - [6]
NQ22FUSWadd more options to `main.js`. update `README.md`. - [7]
CJBGAILAadd optional `--target` parameter for `main.js`. update `README.md`. - [8]
BZ6FC2BTadd `cp.js`. - [9]
6MBUKAG6remove unneeded functions and comments. update readme. - [10]
HLC2L3NJadd "tor.js" and "programs.js". use `minimist` for "main.js" argument parsing. - [11]
FA3U4WUJmore `README.md` changes. - [12]
BCM3TZOSadded `killall.js` option to kill only named scripts. fixed `void_server_buy_or_upgrade` regression that prevented it from working when you don't have servers. - [13]
RWMZ7DVLsplit and refactor various logics. update "README.md". - [14]
SLSWBNYTadded ram utilisation logic to `ram.js` and `servers.js`. split and refactored redundant code into separate library files. - [15]
AXGQ7FMLsplit and refactor hacking logic to "hacker.js" and argument parsing and script execution logic to "main.js". update "README.md". - [*]
7SRULDRFminor refactoring.
Change contents
- file deletion: killall.js
// kills all running scripts in the networkconst void_kill_all_scripts = function (ns) {const array_of_servers = array_get_servers(ns);// exclude host for now so that this script wont be killedarray_of_servers.splice(0, 1);}}// kill scripts in host}}};export const main = async function(ns) {};const array_arguments = ns.args;void_kill_all_scripts(ns, array_arguments);if (array_arguments.length > 0) {for (let integer_indices_5 = 0; integer_indices_5 < array_arguments.length; ++integer_indices_5) {if (array_arguments[integer_indices_5] != ns.getScriptName()) {ns.scriptKill(array_arguments[integer_indices_5], string_host);}}}else {const array_scripts_in_host = ns.ps(string_host);for (let integer_indices_2 = 0; integer_indices_2 < array_scripts_in_host.length; ++integer_indices_2) {// ensure current this script is killed lastif (array_scripts_in_host[integer_indices_2].filename != ns.getScriptName()) {ns.scriptKill(array_scripts_in_host[integer_indices_2].filename, string_host);}else {const array_scripts_in_server = ns.ps(string_server);for (let integer_indices_1 = 0; integer_indices_1 < array_scripts_in_server.length; ++integer_indices_1) {ns.scriptKill(array_scripts_in_server[integer_indices_1].filename, string_server);}}for (let integer_indices_0 = 0; integer_indices_0 < array_of_servers.length; ++integer_indices_0) {const string_server = array_of_servers[integer_indices_0];// if arguments exist, kill the scripts with the same name in the server, otherwise, kill all scripts in the serverif (array_arguments.length > 0) {for (let integer_indices_4 = 0; integer_indices_4 < array_arguments.length; ++integer_indices_4) {ns.scriptKill(array_arguments[integer_indices_4], string_server);}const array_arguments = Array.from(arguments)[1];const string_host = ns.getHostname();import {array_get_servers}from "lib_servers.js";// killall.js - 3.05GB - TODO:// * implement a loop that repeats logic until all appropriate scripts have actually been killed.// * use kill instead of scriptKill. maybe separate that into a lib file that main.js will also use. - edit in bin/main.js at line 6[17.1927]
* add a way to determine and notify if an unrecognised argument was passed*/import {array_get_servers}from "lib_servers.js";import {array_get_files_to_copy}from "lib_ls.js";import {float_get_server_ram_free,array_get_servers_rooted_sorted_by_ram}from "lib_ram_server.js";import {boolean_can_server_run_script_threads}from "lib_ram_script.js";import {void_kill_script_named_server_named}from "lib_kill.js";import {object_parse_arguments}from "lib_minimist.js";// mainexport const main = async function(ns) {// variables// helper scriptsconst object_helpers = object_get_constants().object_helpers;// threads of "nop.js" required to reserve enough RAM to run "hacker.js"const integer_threads_nop = integer_get_threads_nop(ns, object_helpers.string_hacker, ns.getScriptName());// if this server doesn't have enough RAM to run "hacker.js", ejectif (!boolean_can_server_run_script_threads(ns, float_get_server_ram_free(ns, ns.getHostname()), object_helpers.string_nop, integer_threads_nop)) {const string_message_error = `This server has insufficient RAM to run \"${object_helpers.string_nop}\" with \"${integer_threads_nop}\" thread(s).`;ns.tprint(`ERROR: ${string_message_error}`);throw new Error(string_message_error);}// name of purchased serverslet string_servers_bought_name = "server";// maximum amount of jobs to spawn per cycle, used to prevent using up too much IRL RAMlet integer_job_cap = 1000;// duration between each joblet float_padding_seconds = 1;// precision of the percentage to steal calculatorlet float_precision = 0.001;// the maximum percentage of cash that should be stolen from a serverlet float_steal_cap = 0.9;// time period used for checking the time in secondslet float_period_check_seconds = 10;// targetlet string_server_target = "";// ram utilisiation threshold. upgrade ram when reached.let float_ram_utilisation_threshold = 0.9;// argument parsing// optionsconst object_arguments = object_parse_arguments(ns.args);for (const string_argument in object_arguments) {if (object_arguments.hasOwnProperty(string_argument)) {const argument_value = object_arguments[string_argument];switch (string_argument) {// optionscase "c":// fall-throughcase "check-delay":float_period_check_seconds = argument_value;break;case "d":// fall-throughcase "job-delay":float_padding_seconds = argument_value;break;case "i":// fall-throughcase "target":string_server_target = argument_value;break;case "j":// fall-throughcase "job-cap":integer_job_cap = argument_value;break;case "n":// fall-throughcase "server-name":string_servers_bought_name = argument_value;break;case "p":// fall-throughcase "precision":float_precision = argument_value;break;case "r":// fall-throughcase "ram-utilisation":float_ram_utilisation_threshold = argument_value;break;case "s":// fall-throughcase "steal-cap":float_steal_cap = argument_value;break;}}}let array_helpers = [{file: object_helpers.string_ram,threads: 1,args: [float_period_check_seconds, float_ram_utilisation_threshold]},{file: object_helpers.string_servers,threads: 1,args: [float_period_check_seconds, string_servers_bought_name, float_ram_utilisation_threshold]},{file: object_helpers.string_tor,threads: 1,args: [float_period_check_seconds]},{file: object_helpers.string_programs,threads: 1,args: [float_period_check_seconds, ["BruteSSH.exe", "FTPCrack.exe", "relaySMTP.exe", "HTTPWorm.exe", "SQLInject.exe", "DeepscanV1.exe", "DeepscanV2.exe", "Autolink.exe"]]},{file: object_helpers.string_botnet,threads: 1,args: [float_period_check_seconds]}];// flagsfor (const string_argument in object_arguments) {if (object_arguments.hasOwnProperty(string_argument)) {const argument_value = object_arguments[string_argument];if ((string_argument === "a" && argument_value) ||(string_argument === "ram" && !argument_value)) {array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_ram), 1);}if ((string_argument === "e" && argument_value) ||(string_argument === "servers" && !argument_value)) {array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_servers), 1);}if ((string_argument === "o" && argument_value) ||(string_argument === "tor" && !argument_value)) {array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_tor), 1);}if ((string_argument === "g" && argument_value) ||(string_argument === "programs" && !argument_value)) {array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_programs), 1);}if ((string_argument === "b" && argument_value) ||(string_argument === "botnet" && !argument_value)) {array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_botnet), 1);}}}// main// reserve enough RAM for "hacker.js"ns.exec(object_helpers.string_nop, ns.getHostname(), integer_threads_nop);void_copy_scripts(ns);void_script_executor(ns, array_helpers);// kill "nop.js" scripts to free RAMvoid_kill_script_named_server_named(ns, object_helpers.string_nop, ns.getHostname());ns.spawn(object_helpers.string_hacker, 1, float_period_check_seconds, integer_job_cap, float_precision, float_steal_cap, float_padding_seconds, string_server_target);};// functionsconst object_get_constants = function() {return {// helper scriptsobject_helpers: {string_nop: "nop.js",string_hacker: "hacker.js",string_ram: "ram.js",string_servers: "servers.js",string_tor: "tor.js",string_programs: "programs.js",string_botnet: "botnet.js"}};};// return the difference in RAM requirements between two scriptsconst float_get_ram_difference = function(ns, string_script_0, string_script_1) {return ns.getScriptRam(string_script_0) - ns.getScriptRam(string_script_1);};// return the amount of threads of "nop.js" is required to make up RAM difference between two scriptsconst integer_get_threads_nop = function(ns, string_script_0, string_script_1) {return Math.ceil(float_get_ram_difference(ns, string_script_0, string_script_1) / ns.getScriptRam(object_get_constants().object_helpers.string_nop));};// copy all scripts from the current server to all serversconst void_copy_scripts = function(ns) {const string_host = ns.getHostname();const array_script_extensions = [".js", ".ns", ".script"];for (let integer_indices_0 = 0; integer_indices_0 < array_script_extensions.length; ++integer_indices_0) {const string_extension = array_script_extensions[integer_indices_0];void_copy_files_to_servers(ns, array_get_files_to_copy(ns, string_host, string_extension), string_host);}};// returns the index of the scripts array which matches the filename inputconst integer_get_index_of_file = function(array_scripts, string_file) {for (let integer_indices_0 = 0; integer_indices_0 < array_scripts.length; ++integer_indices_0) {const object_script = array_scripts[integer_indices_0];const string_script_file = object_script.file;if (string_script_file === string_file) {return integer_indices_0;}} - replacement in README.md at line 25
`run main.js [OPTIONS] [FLAGS]``run main.js [OPTIONS ...] [FLAGS ...]` - replacement in README.md at line 92
### "killall.js" (3.05 GB)### "kill.js" (2.55 GB) - replacement in README.md at line 94
* Optionally, kill named scripts passed as arguments instead.* Optionally, kill only named scripts instead.* Optionally, kill only scripts on named servers instead.* Optionally, kill only named scripts on named servers instead. - replacement in README.md at line 99
`run killall.js [ARGUMENT ...]``run kill.js [OPTIONS ...]`#### OPTIONS`-c, --script <SCRIPT>`* SCRIPT = The name of a scripts to kill.`-e, --server <SERVER>` - replacement in README.md at line 108
* ARGUMENT = Name of the script to kill. To kill more than one named script, pass them all as arguments to the command, each separated by a space.* SERVER = The name of a server on which running scripts will be killed. - replacement in README.md at line 111
`run killall.js grow.js hack.js``run kill.js`* Kills all running scripts.`run kill.js -c grow.js --script hack.js` - edit in README.md at line 118
`run kill.js -e home --server harakiri-sushi`* Kills all scripts currently running on the "home" and "harakiri-sushi" servers.`run kill.js -c grow.js --script hack.js -e home --server harakiri-sushi`* Kills all scripts named "grow.js" and "hack.js" currently running on the "home" and "harakiri-sushi" servers.