add missing "nop.js". killall.js: rename to kill.js. add `--script` and `--server` options.

[?]
Apr 27, 2020, 5:29 PM
DJDWBCCQHQRKHGRKKQ32QY3WDEM7EKDDQ2YQLSSCS7HZTLHDRCFQC

Dependencies

  • [2] Y5OWMCQR all: 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] 4ACCU75N improve and refactor scheduling logic.
  • [4] 3NFCZ6IP fixed the ram utilisation logic. added flags to `main.js` that can prevent the execution of helpers.
  • [5] 2BKHJI2S init
  • [6] NQ22FUSW add more options to `main.js`. update `README.md`.
  • [7] CJBGAILA add optional `--target` parameter for `main.js`. update `README.md`.
  • [8] BZ6FC2BT add `cp.js`.
  • [9] 6MBUKAG6 remove unneeded functions and comments. update readme.
  • [10] HLC2L3NJ add "tor.js" and "programs.js". use `minimist` for "main.js" argument parsing.
  • [11] FA3U4WUJ more `README.md` changes.
  • [12] BCM3TZOS added `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] RWMZ7DVL split and refactor various logics. update "README.md".
  • [14] SLSWBNYT added ram utilisation logic to `ram.js` and `servers.js`. split and refactored redundant code into separate library files.
  • [15] AXGQ7FML split and refactor hacking logic to "hacker.js" and argument parsing and script execution logic to "main.js". update "README.md".
  • [*] 7SRULDRF minor refactoring.

Change contents

  • file deletion: killall.js (----------)
    [3.3788021][3.3798769:3798803](),[3.3798803][3.3797120:3797120]()
    // kills all running scripts in the network
    const void_kill_all_scripts = function (ns) {
    const array_of_servers = array_get_servers(ns);
    // exclude host for now so that this script wont be killed
    array_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 last
    if (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 server
    if (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";
    // main
    export const main = async function(ns) {
    // variables
    // helper scripts
    const 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", eject
    if (!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 servers
    let string_servers_bought_name = "server";
    // maximum amount of jobs to spawn per cycle, used to prevent using up too much IRL RAM
    let integer_job_cap = 1000;
    // duration between each job
    let float_padding_seconds = 1;
    // precision of the percentage to steal calculator
    let float_precision = 0.001;
    // the maximum percentage of cash that should be stolen from a server
    let float_steal_cap = 0.9;
    // time period used for checking the time in seconds
    let float_period_check_seconds = 10;
    // target
    let string_server_target = "";
    // ram utilisiation threshold. upgrade ram when reached.
    let float_ram_utilisation_threshold = 0.9;
    // argument parsing
    // options
    const 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) {
    // options
    case "c":
    // fall-through
    case "check-delay":
    float_period_check_seconds = argument_value;
    break;
    case "d":
    // fall-through
    case "job-delay":
    float_padding_seconds = argument_value;
    break;
    case "i":
    // fall-through
    case "target":
    string_server_target = argument_value;
    break;
    case "j":
    // fall-through
    case "job-cap":
    integer_job_cap = argument_value;
    break;
    case "n":
    // fall-through
    case "server-name":
    string_servers_bought_name = argument_value;
    break;
    case "p":
    // fall-through
    case "precision":
    float_precision = argument_value;
    break;
    case "r":
    // fall-through
    case "ram-utilisation":
    float_ram_utilisation_threshold = argument_value;
    break;
    case "s":
    // fall-through
    case "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]
    }
    ];
    // flags
    for (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 RAM
    void_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);
    };
    // functions
    const object_get_constants = function() {
    return {
    // helper scripts
    object_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 scripts
    const 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 scripts
    const 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 servers
    const 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 input
    const 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
    [3.3327][3.7222:7256]()
    `run main.js [OPTIONS] [FLAGS]`
    [3.3327]
    [3.3353]
    `run main.js [OPTIONS ...] [FLAGS ...]`
  • replacement in README.md at line 92
    [3.5169][3.45260:45287]()
    ### "killall.js" (3.05 GB)
    [3.5169]
    [3.3528]
    ### "kill.js" (2.55 GB)
  • replacement in README.md at line 94
    [3.3558][3.3096:3160]()
    * Optionally, kill named scripts passed as arguments instead.
    [3.3558]
    [3.3558]
    * 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
    [3.5208][3.5208:5242]()
    `run killall.js [ARGUMENT ...]`
    [3.5208]
    [3.5242]
    `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
    [3.5243][3.5243:5392]()
    * 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.
    [3.5243]
    [3.5392]
    * SERVER = The name of a server on which running scripts will be killed.
  • replacement in README.md at line 111
    [3.5407][3.5407:5442]()
    `run killall.js grow.js hack.js`
    [3.5407]
    [3.5442]
    `run kill.js`
    * Kills all running scripts.
    `run kill.js -c grow.js --script hack.js`
  • edit in README.md at line 118
    [3.6837]
    [3.5546]
    `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.