2019-04-05 11:08:41 +02:00
|
|
|
import {
|
|
|
|
evaluateDirectoryPath,
|
2019-04-10 08:07:12 +02:00
|
|
|
evaluateFilePath,
|
|
|
|
getFirstParentDirectory,
|
|
|
|
isInRootDirectory,
|
2019-04-05 11:08:41 +02:00
|
|
|
isValidDirectoryPath,
|
2019-04-10 08:07:12 +02:00
|
|
|
removeLeadingSlash,
|
2021-04-30 05:52:56 +02:00
|
|
|
removeTrailingSlash,
|
2019-04-10 08:07:12 +02:00
|
|
|
} from "./Terminal/DirectoryHelpers";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { determineAllPossibilitiesForTabCompletion } from "./Terminal/determineAllPossibilitiesForTabCompletion";
|
|
|
|
import { TerminalHelpText, HelpTexts } from "./Terminal/HelpText";
|
|
|
|
import { tabCompletion } from "./Terminal/tabCompletion";
|
2021-05-17 23:12:35 +02:00
|
|
|
import { createFconf } from "./Fconf/Fconf";
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
import {
|
2019-04-11 10:37:40 +02:00
|
|
|
parseAliasDeclaration,
|
|
|
|
printAliases,
|
|
|
|
removeAlias,
|
2021-04-30 05:52:56 +02:00
|
|
|
substituteAliases,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./Alias";
|
|
|
|
import { BitNodeMultipliers } from "./BitNode/BitNodeMultipliers";
|
2019-04-10 08:07:12 +02:00
|
|
|
import {
|
2019-04-11 10:37:40 +02:00
|
|
|
CodingContractResult,
|
|
|
|
} from "./CodingContracts";
|
|
|
|
import { CONSTANTS } from "./Constants";
|
|
|
|
import { Programs } from "./Programs/Programs";
|
2019-04-10 08:07:12 +02:00
|
|
|
import {
|
2019-04-11 10:37:40 +02:00
|
|
|
executeDarkwebTerminalCommand,
|
2021-04-30 05:52:56 +02:00
|
|
|
checkIfConnectedToDarkweb,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./DarkWeb/DarkWeb";
|
|
|
|
import { Engine } from "./engine";
|
|
|
|
import { FconfSettings } from "./Fconf/FconfSettings";
|
|
|
|
import {
|
|
|
|
calculateHackingChance,
|
|
|
|
calculateHackingExpGain,
|
|
|
|
calculatePercentMoneyHacked,
|
|
|
|
calculateHackingTime,
|
|
|
|
calculateGrowTime,
|
2021-04-30 05:52:56 +02:00
|
|
|
calculateWeakenTime,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./Hacking";
|
|
|
|
import { HacknetServer } from "./Hacknet/HacknetServer";
|
|
|
|
import {
|
|
|
|
iTutorialNextStep,
|
|
|
|
iTutorialSteps,
|
2021-04-30 05:52:56 +02:00
|
|
|
ITutorial,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./InteractiveTutorial";
|
2021-03-14 07:08:24 +01:00
|
|
|
import { showLiterature } from "./Literature/LiteratureHelpers";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { Message } from "./Message/Message";
|
|
|
|
import { showMessage } from "./Message/MessageHelpers";
|
2019-07-14 05:55:58 +02:00
|
|
|
import { startWorkerScript } from "./NetscriptWorker";
|
2019-05-16 08:05:36 +02:00
|
|
|
import { killWorkerScript } from "./Netscript/killWorkerScript";
|
2019-06-27 09:01:06 +02:00
|
|
|
import { WorkerScriptStartStopEventEmitter } from "./Netscript/WorkerScriptStartStopEventEmitter";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { Player } from "./Player";
|
|
|
|
import { hackWorldDaemon } from "./RedPill";
|
|
|
|
import { RunningScript } from "./Script/RunningScript";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { getRamUsageFromRunningScript } from "./Script/RunningScriptHelpers";
|
2021-05-02 05:33:44 +02:00
|
|
|
import {
|
|
|
|
getCurrentEditor,
|
|
|
|
findRunningScript,
|
|
|
|
findRunningScriptByPid,
|
|
|
|
} from "./Script/ScriptHelpers";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { isScriptFilename } from "./Script/ScriptHelpersTS";
|
|
|
|
import { AllServers } from "./Server/AllServers";
|
|
|
|
import {
|
|
|
|
GetServerByHostname,
|
|
|
|
getServer,
|
2021-04-30 05:52:56 +02:00
|
|
|
getServerOnNetwork,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./Server/ServerHelpers";
|
|
|
|
import {
|
|
|
|
SpecialServerIps,
|
2021-04-30 05:52:56 +02:00
|
|
|
SpecialServerNames,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./Server/SpecialServerIps";
|
|
|
|
import { setTimeoutRef } from "./utils/SetTimeoutRef";
|
|
|
|
import { Page, routing } from "./ui/navigationTracking";
|
|
|
|
import { numeralWrapper } from "./ui/numeralFormat";
|
|
|
|
import { KEY } from "../utils/helpers/keyCodes";
|
|
|
|
import { arrayToString } from "../utils/helpers/arrayToString";
|
|
|
|
import { getTimestamp } from "../utils/helpers/getTimestamp";
|
|
|
|
import { logBoxCreate } from "../utils/LogBox";
|
|
|
|
import {
|
|
|
|
yesNoBoxCreate,
|
|
|
|
yesNoBoxGetYesButton,
|
|
|
|
yesNoBoxGetNoButton,
|
2021-04-30 05:52:56 +02:00
|
|
|
yesNoBoxClose,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "../utils/YesNoBox";
|
2019-04-10 08:07:12 +02:00
|
|
|
import {
|
|
|
|
post,
|
2021-03-31 06:45:21 +02:00
|
|
|
postElement,
|
2019-04-10 08:07:12 +02:00
|
|
|
postContent,
|
|
|
|
postError,
|
|
|
|
hackProgressBarPost,
|
2021-04-30 05:52:56 +02:00
|
|
|
hackProgressPost,
|
2019-04-10 08:07:12 +02:00
|
|
|
} from "./ui/postToTerminal";
|
2021-03-31 06:45:21 +02:00
|
|
|
import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions";
|
|
|
|
import { Money } from "./ui/React/Money";
|
2016-12-21 19:36:42 +01:00
|
|
|
|
2019-04-11 10:37:40 +02:00
|
|
|
import autosize from "autosize";
|
|
|
|
import * as JSZip from "jszip";
|
|
|
|
import * as FileSaver from "file-saver";
|
2021-03-31 06:45:21 +02:00
|
|
|
import React from "react";
|
2019-04-11 10:37:40 +02:00
|
|
|
|
2018-03-12 20:39:04 +01:00
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function postNetburnerText() {
|
2021-04-18 17:18:56 +02:00
|
|
|
post("Bitburner v" + CONSTANTS.Version);
|
2016-10-20 23:11:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 03:32:15 +01:00
|
|
|
// Helper function that checks if an argument (which is a string) is a valid number
|
|
|
|
function isNumber(str) {
|
|
|
|
if (typeof str != "string") { return false; } // Only process strings
|
|
|
|
return !isNaN(str) && !isNaN(parseFloat(str));
|
|
|
|
}
|
|
|
|
|
2021-03-24 02:30:15 +01:00
|
|
|
function getTerminalInput() {
|
|
|
|
return document.getElementById("terminal-input-text-box").value;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Defines key commands in terminal
|
2017-05-01 23:38:49 +02:00
|
|
|
$(document).keydown(function(event) {
|
2021-04-18 17:18:56 +02:00
|
|
|
// Terminal
|
|
|
|
if (routing.isOn(Page.Terminal)) {
|
2017-05-02 18:28:54 +02:00
|
|
|
var terminalInput = document.getElementById("terminal-input-text-box");
|
2018-09-14 23:03:31 +02:00
|
|
|
if (terminalInput != null && !event.ctrlKey && !event.shiftKey && !Terminal.contractOpen) {terminalInput.focus();}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
if (event.keyCode === KEY.ENTER) {
|
2019-04-13 03:22:46 +02:00
|
|
|
event.preventDefault(); // Prevent newline from being entered in Script Editor
|
2021-04-18 17:18:56 +02:00
|
|
|
const command = getTerminalInput();
|
2019-04-10 08:07:12 +02:00
|
|
|
const dir = Terminal.currDir;
|
2021-04-18 17:18:56 +02:00
|
|
|
post(
|
2018-09-03 14:10:00 +02:00
|
|
|
"<span class='prompt'>[" +
|
2018-08-02 17:04:21 +02:00
|
|
|
(FconfSettings.ENABLE_TIMESTAMPS ? getTimestamp() + " " : "") +
|
2018-07-31 02:16:39 +02:00
|
|
|
Player.getCurrentServer().hostname +
|
2021-04-30 05:52:56 +02:00
|
|
|
` ~${dir}]></span> ${command}`,
|
2018-07-31 02:16:39 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (command.length > 0) {
|
2019-04-13 03:22:46 +02:00
|
|
|
Terminal.resetTerminalInput(); // Clear input first
|
2019-02-05 07:45:04 +01:00
|
|
|
Terminal.executeCommands(command);
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
if (event.keyCode === KEY.C && event.ctrlKey) {
|
2018-03-12 20:39:04 +01:00
|
|
|
if (Engine._actionInProgress) {
|
2019-04-13 03:22:46 +02:00
|
|
|
// Cancel action
|
2018-03-12 20:39:04 +01:00
|
|
|
post("Cancelling...");
|
2021-04-18 17:18:56 +02:00
|
|
|
Engine._actionInProgress = false;
|
|
|
|
Terminal.finishAction(true);
|
2018-03-12 20:39:04 +01:00
|
|
|
} else if (FconfSettings.ENABLE_BASH_HOTKEYS) {
|
2019-04-13 03:22:46 +02:00
|
|
|
// Dont prevent default so it still copies
|
|
|
|
Terminal.resetTerminalInput(); // Clear Terminal
|
2018-03-12 20:39:04 +01:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-03-12 20:39:04 +01:00
|
|
|
if (event.keyCode === KEY.L && event.ctrlKey) {
|
|
|
|
event.preventDefault();
|
2019-04-13 03:22:46 +02:00
|
|
|
Terminal.executeCommand("clear"); // Clear screen
|
2018-03-12 20:39:04 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Ctrl p same as up arrow
|
|
|
|
// Ctrl n same as down arrow
|
2018-03-12 20:39:04 +01:00
|
|
|
|
|
|
|
if (event.keyCode === KEY.UPARROW ||
|
|
|
|
(FconfSettings.ENABLE_BASH_HOTKEYS && event.keyCode === KEY.P && event.ctrlKey)) {
|
|
|
|
if (FconfSettings.ENABLE_BASH_HOTKEYS) {event.preventDefault();}
|
2019-04-13 03:22:46 +02:00
|
|
|
// Cycle through past commands
|
2017-05-03 06:38:58 +02:00
|
|
|
if (terminalInput == null) {return;}
|
2017-05-02 05:05:26 +02:00
|
|
|
var i = Terminal.commandHistoryIndex;
|
|
|
|
var len = Terminal.commandHistory.length;
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-05-02 05:05:26 +02:00
|
|
|
if (len == 0) {return;}
|
|
|
|
if (i < 0 || i > len) {
|
|
|
|
Terminal.commandHistoryIndex = len;
|
2017-07-25 03:06:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 05:05:26 +02:00
|
|
|
if (i != 0) {
|
|
|
|
--Terminal.commandHistoryIndex;
|
|
|
|
}
|
2017-05-01 19:23:20 +02:00
|
|
|
var prevCommand = Terminal.commandHistory[Terminal.commandHistoryIndex];
|
2017-05-02 18:28:54 +02:00
|
|
|
terminalInput.value = prevCommand;
|
2021-04-18 17:18:56 +02:00
|
|
|
setTimeoutRef(function(){terminalInput.selectionStart = terminalInput.selectionEnd = 10000; }, 10);
|
2017-05-02 05:05:26 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-03-12 20:39:04 +01:00
|
|
|
if (event.keyCode === KEY.DOWNARROW ||
|
|
|
|
(FconfSettings.ENABLE_BASH_HOTKEYS && event.keyCode === KEY.M && event.ctrlKey)) {
|
|
|
|
if (FconfSettings.ENABLE_BASH_HOTKEYS) {event.preventDefault();}
|
2019-04-13 03:22:46 +02:00
|
|
|
// Cycle through past commands
|
2017-05-03 06:38:58 +02:00
|
|
|
if (terminalInput == null) {return;}
|
2017-05-02 05:05:26 +02:00
|
|
|
var i = Terminal.commandHistoryIndex;
|
|
|
|
var len = Terminal.commandHistory.length;
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-05-02 05:05:26 +02:00
|
|
|
if (len == 0) {return;}
|
|
|
|
if (i < 0 || i > len) {
|
|
|
|
Terminal.commandHistoryIndex = len;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Latest command, put nothing
|
2017-05-02 05:05:26 +02:00
|
|
|
if (i == len || i == len-1) {
|
|
|
|
Terminal.commandHistoryIndex = len;
|
2017-05-02 18:28:54 +02:00
|
|
|
terminalInput.value = "";
|
2017-05-02 05:05:26 +02:00
|
|
|
} else {
|
|
|
|
++Terminal.commandHistoryIndex;
|
|
|
|
var prevCommand = Terminal.commandHistory[Terminal.commandHistoryIndex];
|
2017-05-02 18:28:54 +02:00
|
|
|
terminalInput.value = prevCommand;
|
2017-05-02 05:05:26 +02:00
|
|
|
}
|
2017-05-01 19:23:20 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-03-12 20:39:04 +01:00
|
|
|
if (event.keyCode === KEY.TAB) {
|
2018-07-16 08:00:57 +02:00
|
|
|
event.preventDefault();
|
2018-07-20 16:28:03 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Autocomplete
|
2017-05-03 06:38:58 +02:00
|
|
|
if (terminalInput == null) {return;}
|
2019-04-20 07:27:33 +02:00
|
|
|
let input = terminalInput.value;
|
|
|
|
if (input == "") { return; }
|
2018-07-31 02:16:39 +02:00
|
|
|
|
|
|
|
const semiColonIndex = input.lastIndexOf(";");
|
|
|
|
if(semiColonIndex !== -1) {
|
2019-02-09 03:46:30 +01:00
|
|
|
input = input.slice(semiColonIndex + 1);
|
2018-07-31 02:16:39 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 05:05:26 +02:00
|
|
|
input = input.trim();
|
2017-05-01 23:38:49 +02:00
|
|
|
input = input.replace(/\s\s+/g, ' ');
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-20 07:27:33 +02:00
|
|
|
const commandArray = input.split(" ");
|
|
|
|
let index = commandArray.length - 2;
|
|
|
|
if (index < -1) { index = 0; }
|
|
|
|
const allPos = determineAllPossibilitiesForTabCompletion(Player, input, index, Terminal.currDir);
|
2017-05-01 23:38:49 +02:00
|
|
|
if (allPos.length == 0) {return;}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-20 07:27:33 +02:00
|
|
|
let arg = "";
|
|
|
|
let command = "";
|
2017-05-01 23:38:49 +02:00
|
|
|
if (commandArray.length == 0) {return;}
|
2017-07-25 03:06:40 +02:00
|
|
|
if (commandArray.length == 1) {command = commandArray[0];}
|
2017-05-30 04:02:41 +02:00
|
|
|
else if (commandArray.length == 2) {
|
|
|
|
command = commandArray[0];
|
2017-05-01 23:38:49 +02:00
|
|
|
arg = commandArray[1];
|
2017-05-30 04:02:41 +02:00
|
|
|
} else if (commandArray.length == 3) {
|
|
|
|
command = commandArray[0] + " " + commandArray[1];
|
|
|
|
arg = commandArray[2];
|
|
|
|
} else {
|
2017-07-25 03:06:40 +02:00
|
|
|
arg = commandArray.pop();
|
|
|
|
command = commandArray.join(" ");
|
2017-05-01 23:38:49 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-05-30 04:02:41 +02:00
|
|
|
tabCompletion(command, arg, allPos);
|
2018-07-16 08:00:57 +02:00
|
|
|
terminalInput.focus();
|
2017-05-01 23:38:49 +02:00
|
|
|
}
|
2018-03-12 20:39:04 +01:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Extra Bash Emulation Hotkeys, must be enabled through .fconf
|
2018-03-12 20:39:04 +01:00
|
|
|
if (FconfSettings.ENABLE_BASH_HOTKEYS) {
|
|
|
|
if (event.keyCode === KEY.A && event.ctrlKey) {
|
|
|
|
event.preventDefault();
|
|
|
|
Terminal.moveTextCursor("home");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode === KEY.E && event.ctrlKey) {
|
|
|
|
event.preventDefault();
|
|
|
|
Terminal.moveTextCursor("end");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode === KEY.B && event.ctrlKey) {
|
|
|
|
event.preventDefault();
|
|
|
|
Terminal.moveTextCursor("prevchar");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode === KEY.B && event.altKey) {
|
|
|
|
event.preventDefault();
|
|
|
|
Terminal.moveTextCursor("prevword");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode === KEY.F && event.ctrlKey) {
|
|
|
|
event.preventDefault();
|
|
|
|
Terminal.moveTextCursor("nextchar");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode === KEY.F && event.altKey) {
|
|
|
|
event.preventDefault();
|
|
|
|
Terminal.moveTextCursor("nextword");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((event.keyCode === KEY.H || event.keyCode === KEY.D) && event.ctrlKey) {
|
|
|
|
Terminal.modifyInput("backspace");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// TODO AFTER THIS:
|
|
|
|
// alt + d deletes word after cursor
|
|
|
|
// ^w deletes word before cursor
|
|
|
|
// ^k clears line after cursor
|
|
|
|
// ^u clears line before cursor
|
2018-03-12 20:39:04 +01:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
2016-10-17 10:24:39 +02:00
|
|
|
});
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Keep terminal in focus
|
2018-05-06 22:27:47 +02:00
|
|
|
let terminalCtrlPressed = false, shiftKeyPressed = false;
|
2016-11-24 23:30:33 +01:00
|
|
|
$(document).ready(function() {
|
2021-04-18 17:18:56 +02:00
|
|
|
if (routing.isOn(Page.Terminal)) {
|
|
|
|
$('.terminal-input').focus();
|
|
|
|
}
|
2016-11-24 23:30:33 +01:00
|
|
|
});
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
$(document).keydown(function(e) {
|
2021-04-18 17:18:56 +02:00
|
|
|
if (routing.isOn(Page.Terminal)) {
|
|
|
|
if (e.which == KEY.CTRL) {
|
|
|
|
terminalCtrlPressed = true;
|
|
|
|
} else if (e.shiftKey) {
|
2018-05-06 22:27:47 +02:00
|
|
|
shiftKeyPressed = true;
|
2018-09-14 23:03:31 +02:00
|
|
|
} else if (terminalCtrlPressed || shiftKeyPressed || Terminal.contractOpen) {
|
2021-04-18 17:18:56 +02:00
|
|
|
// Don't focus
|
|
|
|
} else {
|
2017-05-03 06:38:58 +02:00
|
|
|
var inputTextBox = document.getElementById("terminal-input-text-box");
|
2018-05-06 22:27:47 +02:00
|
|
|
if (inputTextBox != null) {inputTextBox.focus();}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
terminalCtrlPressed = false;
|
2018-05-06 22:27:47 +02:00
|
|
|
shiftKeyPressed = false;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
});
|
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
$(document).keyup(function(e) {
|
2021-04-18 17:18:56 +02:00
|
|
|
if (routing.isOn(Page.Terminal)) {
|
|
|
|
if (e.which == KEY.CTRL) {
|
|
|
|
terminalCtrlPressed = false;
|
|
|
|
}
|
2018-05-06 22:27:47 +02:00
|
|
|
if (e.shiftKey) {
|
|
|
|
shiftKeyPressed = false;
|
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
});
|
2017-05-01 23:38:49 +02:00
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
let Terminal = {
|
2019-04-05 11:08:41 +02:00
|
|
|
// Flags to determine whether the player is currently running a hack or an analyze
|
2018-08-30 19:00:38 +02:00
|
|
|
hackFlag: false,
|
2021-04-29 02:07:26 +02:00
|
|
|
backdoorFlag: false,
|
2018-08-30 19:00:38 +02:00
|
|
|
analyzeFlag: false,
|
|
|
|
actionStarted: false,
|
|
|
|
actionTime: 0,
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-05-01 19:23:20 +02:00
|
|
|
commandHistory: [],
|
|
|
|
commandHistoryIndex: 0,
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
// True if a Coding Contract prompt is opened
|
|
|
|
contractOpen: false,
|
|
|
|
|
|
|
|
// Full Path of current directory
|
|
|
|
// Excludes the trailing forward slash
|
2019-04-10 08:07:12 +02:00
|
|
|
currDir: "/",
|
2018-09-14 23:03:31 +02:00
|
|
|
|
2021-03-24 02:30:15 +01:00
|
|
|
resetTerminalInput: function(keepInput=false) {
|
|
|
|
let input = "";
|
|
|
|
if(keepInput) {
|
|
|
|
input = getTerminalInput();
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
const dir = Terminal.currDir;
|
2018-08-29 05:24:38 +02:00
|
|
|
if (FconfSettings.WRAP_INPUT) {
|
|
|
|
document.getElementById("terminal-input-td").innerHTML =
|
2019-04-10 08:07:12 +02:00
|
|
|
`<div id='terminal-input-header' class='prompt'>[${Player.getCurrentServer().hostname} ~${dir}]$ </div>` +
|
2021-04-18 17:18:56 +02:00
|
|
|
`<textarea type="text" id="terminal-input-text-box" class="terminal-input" tabindex="1" value=\"${input}\" autocomplete="off" />`;
|
2018-08-29 05:24:38 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Auto re-size the line element as it wraps
|
2018-08-29 05:24:38 +02:00
|
|
|
autosize(document.getElementById("terminal-input-text-box"));
|
|
|
|
} else {
|
|
|
|
document.getElementById("terminal-input-td").innerHTML =
|
2019-04-10 08:07:12 +02:00
|
|
|
`<div id='terminal-input-header' class='prompt'>[${Player.getCurrentServer().hostname} ~${dir}]$ </div>` +
|
2021-04-18 17:18:56 +02:00
|
|
|
`<input type="text" id="terminal-input-text-box" class="terminal-input" tabindex="1" value=\"${input}\" autocomplete="off" />`;
|
2018-08-29 05:24:38 +02:00
|
|
|
}
|
2021-03-24 02:30:15 +01:00
|
|
|
const hdr = document.getElementById("terminal-input-header");
|
2017-09-13 16:22:22 +02:00
|
|
|
hdr.style.display = "inline";
|
2021-03-24 02:30:15 +01:00
|
|
|
|
|
|
|
const terminalInput = document.getElementById("terminal-input-text-box");
|
|
|
|
if (typeof terminalInput.selectionStart == "number") {
|
|
|
|
terminalInput.selectionStart = terminalInput.selectionEnd = terminalInput.value.length;
|
|
|
|
} else if (typeof terminalInput.createTextRange != "undefined") {
|
|
|
|
terminalInput.focus();
|
|
|
|
var range = el.createTextRange();
|
|
|
|
range.collapse(false);
|
|
|
|
range.select();
|
|
|
|
}
|
2018-03-12 20:39:04 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
modifyInput: function(mod) {
|
|
|
|
try {
|
|
|
|
var terminalInput = document.getElementById("terminal-input-text-box");
|
|
|
|
if (terminalInput == null) {return;}
|
|
|
|
terminalInput.focus();
|
|
|
|
|
|
|
|
var inputLength = terminalInput.value.length;
|
|
|
|
var start = terminalInput.selectionStart;
|
|
|
|
var inputText = terminalInput.value;
|
|
|
|
|
|
|
|
switch(mod.toLowerCase()) {
|
|
|
|
case "backspace":
|
|
|
|
if (start > 0 && start <= inputLength+1) {
|
|
|
|
terminalInput.value = inputText.substr(0, start-1) + inputText.substr(start);
|
|
|
|
}
|
|
|
|
break;
|
2019-04-13 03:22:46 +02:00
|
|
|
case "deletewordbefore": // Delete rest of word before the cursor
|
2018-03-12 20:39:04 +01:00
|
|
|
for (var delStart = start-1; delStart > 0; --delStart) {
|
|
|
|
if (inputText.charAt(delStart) === " ") {
|
|
|
|
terminalInput.value = inputText.substr(0, delStart) + inputText.substr(start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-04-13 03:22:46 +02:00
|
|
|
case "deletewordafter": // Delete rest of word after the cursor
|
2018-03-12 20:39:04 +01:00
|
|
|
for (var delStart = start+1; delStart <= text.length+1; ++delStart) {
|
|
|
|
if (inputText.charAt(delStart) === " ") {
|
|
|
|
terminalInput.value = inputText.substr(0, start) + inputText.substr(delStart);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-04-13 03:22:46 +02:00
|
|
|
case "clearafter": // Deletes everything after cursor
|
2018-03-12 20:39:04 +01:00
|
|
|
break;
|
2019-04-13 03:22:46 +02:00
|
|
|
case "clearbefore:": // Deleetes everything before cursor
|
2018-03-12 20:39:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch(e) {
|
2019-02-12 01:23:46 +01:00
|
|
|
console.error("Exception in Terminal.modifyInput: " + e);
|
2018-03-12 20:39:04 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
moveTextCursor: function(loc) {
|
|
|
|
try {
|
|
|
|
var terminalInput = document.getElementById("terminal-input-text-box");
|
|
|
|
if (terminalInput == null) {return;}
|
|
|
|
terminalInput.focus();
|
|
|
|
|
|
|
|
var inputLength = terminalInput.value.length;
|
|
|
|
var start = terminalInput.selectionStart;
|
|
|
|
|
|
|
|
switch(loc.toLowerCase()) {
|
|
|
|
case "home":
|
|
|
|
terminalInput.setSelectionRange(0,0);
|
|
|
|
break;
|
|
|
|
case "end":
|
|
|
|
terminalInput.setSelectionRange(inputLength, inputLength);
|
|
|
|
break;
|
|
|
|
case "prevchar":
|
|
|
|
if (start > 0) {terminalInput.setSelectionRange(start-1, start-1);}
|
|
|
|
break;
|
|
|
|
case "prevword":
|
|
|
|
for (var i = start-2; i >= 0; --i) {
|
|
|
|
if (terminalInput.value.charAt(i) === " ") {
|
|
|
|
terminalInput.setSelectionRange(i+1, i+1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
terminalInput.setSelectionRange(0, 0);
|
|
|
|
break;
|
|
|
|
case "nextchar":
|
|
|
|
terminalInput.setSelectionRange(start+1, start+1);
|
|
|
|
break;
|
|
|
|
case "nextword":
|
|
|
|
for (var i = start+1; i <= inputLength; ++i) {
|
|
|
|
if (terminalInput.value.charAt(i) === " ") {
|
|
|
|
terminalInput.setSelectionRange(i, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
terminalInput.setSelectionRange(inputLength, inputLength);
|
|
|
|
break;
|
|
|
|
default:
|
2019-02-12 01:23:46 +01:00
|
|
|
console.warn("Invalid loc argument in Terminal.moveTextCursor()");
|
2018-03-12 20:39:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch(e) {
|
2019-02-12 01:23:46 +01:00
|
|
|
console.error("Exception in Terminal.moveTextCursor: " + e);
|
2018-03-12 20:39:04 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-30 19:00:38 +02:00
|
|
|
startHack: function() {
|
|
|
|
Terminal.hackFlag = true;
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Hacking through Terminal should be faster than hacking through a script
|
2021-03-31 06:45:21 +02:00
|
|
|
Terminal.actionTime = calculateHackingTime(Player.getCurrentServer(), Player) / 4;
|
2018-08-30 19:00:38 +02:00
|
|
|
Terminal.startAction();
|
|
|
|
},
|
|
|
|
|
2021-04-29 02:07:26 +02:00
|
|
|
startBackdoor: function() {
|
|
|
|
Terminal.backdoorFlag = true;
|
|
|
|
|
|
|
|
// Backdoor should take the same amount of time as hack
|
|
|
|
Terminal.actionTime = calculateHackingTime(Player.getCurrentServer(), Player) / 4;
|
|
|
|
Terminal.startAction();
|
|
|
|
},
|
|
|
|
|
2018-08-30 19:00:38 +02:00
|
|
|
startAnalyze: function() {
|
|
|
|
Terminal.analyzeFlag = true;
|
|
|
|
Terminal.actionTime = 1;
|
|
|
|
post("Analyzing system...");
|
|
|
|
Terminal.startAction();
|
|
|
|
},
|
|
|
|
|
|
|
|
startAction: function() {
|
|
|
|
Terminal.actionStarted = true;
|
|
|
|
|
|
|
|
hackProgressPost("Time left:");
|
|
|
|
hackProgressBarPost("[");
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Disable terminal
|
2018-08-30 19:00:38 +02:00
|
|
|
document.getElementById("terminal-input-td").innerHTML = '<input type="text" class="terminal-input"/>';
|
|
|
|
$('input[class=terminal-input]').prop('disabled', true);
|
|
|
|
},
|
|
|
|
|
2018-03-12 20:39:04 +01:00
|
|
|
finishAction: function(cancelled = false) {
|
|
|
|
if (Terminal.hackFlag) {
|
|
|
|
Terminal.finishHack(cancelled);
|
2021-04-29 02:07:26 +02:00
|
|
|
} else if (Terminal.backdoorFlag) {
|
|
|
|
Terminal.finishBackdoor(cancelled);
|
2018-03-12 20:39:04 +01:00
|
|
|
} else if (Terminal.analyzeFlag) {
|
|
|
|
Terminal.finishAnalyze(cancelled);
|
|
|
|
}
|
2021-04-29 02:07:26 +02:00
|
|
|
|
|
|
|
// Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal
|
|
|
|
$("#hack-progress-bar").attr('id', "old-hack-progress-bar");
|
|
|
|
$("#hack-progress").attr('id', "old-hack-progress");
|
|
|
|
Terminal.resetTerminalInput();
|
|
|
|
$('input[class=terminal-input]').prop('disabled', false);
|
2017-09-13 16:22:22 +02:00
|
|
|
},
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Complete the hack/analyze command
|
2021-04-29 02:07:26 +02:00
|
|
|
finishHack: function(cancelled = false) {
|
|
|
|
if (!cancelled) {
|
2017-06-02 06:15:45 +02:00
|
|
|
var server = Player.getCurrentServer();
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// Calculate whether hack was successful
|
|
|
|
var hackChance = calculateHackingChance(server, Player);
|
|
|
|
var rand = Math.random();
|
|
|
|
var expGainedOnSuccess = calculateHackingExpGain(server, Player);
|
|
|
|
var expGainedOnFailure = (expGainedOnSuccess / 4);
|
|
|
|
if (rand < hackChance) { // Success!
|
2017-07-22 00:54:55 +02:00
|
|
|
if (SpecialServerIps[SpecialServerNames.WorldDaemon] &&
|
|
|
|
SpecialServerIps[SpecialServerNames.WorldDaemon] == server.ip) {
|
2017-08-13 07:01:33 +02:00
|
|
|
if (Player.bitNodeN == null) {
|
|
|
|
Player.bitNodeN = 1;
|
|
|
|
}
|
|
|
|
hackWorldDaemon(Player.bitNodeN);
|
2021-05-06 17:12:04 +02:00
|
|
|
Terminal.hackFlag = false;
|
2017-07-22 00:54:55 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-04-29 02:07:26 +02:00
|
|
|
server.backdoorInstalled = true;
|
2021-04-18 17:18:56 +02:00
|
|
|
var moneyGained = calculatePercentMoneyHacked(server, Player);
|
|
|
|
moneyGained = Math.floor(server.moneyAvailable * moneyGained);
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
if (moneyGained <= 0) {moneyGained = 0;} // Safety check
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
server.moneyAvailable -= moneyGained;
|
|
|
|
Player.gainMoney(moneyGained);
|
2019-02-22 03:26:28 +01:00
|
|
|
Player.recordMoneySource(moneyGained, "hacking");
|
2017-04-18 06:32:17 +02:00
|
|
|
Player.gainHackingExp(expGainedOnSuccess)
|
2017-09-15 16:06:59 +02:00
|
|
|
Player.gainIntelligenceExp(expGainedOnSuccess / CONSTANTS.IntelligenceTerminalHackBaseExpGain);
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-06-02 06:15:45 +02:00
|
|
|
server.fortify(CONSTANTS.ServerFortifyAmount);
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
postElement(<>Hack successful! Gained {Money(moneyGained)} and {numeralWrapper.formatExp(expGainedOnSuccess)} hacking exp</>);
|
|
|
|
} else { // Failure
|
|
|
|
// Player only gains 25% exp for failure? TODO Can change this later to balance
|
2017-04-18 06:32:17 +02:00
|
|
|
Player.gainHackingExp(expGainedOnFailure)
|
2021-04-29 02:07:26 +02:00
|
|
|
post(`Failed to hack ${server.hostname}. Gained ${numeralWrapper.formatExp(expGainedOnFailure)} hacking exp`);
|
|
|
|
}
|
|
|
|
}
|
2016-11-01 06:30:59 +01:00
|
|
|
Terminal.hackFlag = false;
|
2021-04-18 17:18:56 +02:00
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-29 02:07:26 +02:00
|
|
|
finishBackdoor: function(cancelled = false) {
|
|
|
|
if(!cancelled){
|
2021-05-04 01:00:01 +02:00
|
|
|
const server = Player.getCurrentServer();
|
|
|
|
if (SpecialServerIps[SpecialServerNames.WorldDaemon] &&
|
|
|
|
SpecialServerIps[SpecialServerNames.WorldDaemon] == server.ip) {
|
|
|
|
if (Player.bitNodeN == null) {
|
|
|
|
Player.bitNodeN = 1;
|
|
|
|
}
|
|
|
|
hackWorldDaemon(Player.bitNodeN);
|
2021-05-06 17:12:04 +02:00
|
|
|
Terminal.backdoorFlag = false;
|
2021-05-04 01:00:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-04-29 02:07:26 +02:00
|
|
|
server.backdoorInstalled = true;
|
|
|
|
postElement(<>Backdoor successful!</>);
|
|
|
|
}
|
|
|
|
Terminal.backdoorFlag = false;
|
|
|
|
},
|
|
|
|
|
2016-12-21 17:33:00 +01:00
|
|
|
finishAnalyze: function(cancelled = false) {
|
2021-04-29 02:07:26 +02:00
|
|
|
if (!cancelled) {
|
2018-08-30 19:00:38 +02:00
|
|
|
let currServ = Player.getCurrentServer();
|
2019-03-25 04:03:24 +01:00
|
|
|
const isHacknet = currServ instanceof HacknetServer;
|
2021-04-18 17:18:56 +02:00
|
|
|
post(currServ.hostname + ": ");
|
2021-05-03 08:20:31 +02:00
|
|
|
const org = currServ.organizationName
|
|
|
|
post("Organization name: " + (!isHacknet ? org : "Player"));
|
|
|
|
const admin = currServ.hasAdminRights;
|
2021-05-06 01:15:56 +02:00
|
|
|
let hasAdminRights = !isHacknet && currServ.hasAdminRights || isHacknet;
|
|
|
|
post("Root Access: " + (hasAdminRights ? "YES" : "NO"));
|
2021-05-03 08:20:31 +02:00
|
|
|
const hackingSkill = currServ.requiredHackingSkill
|
|
|
|
post("Required hacking skill: " + (!isHacknet ? hackingSkill : "N/A"));
|
|
|
|
const security = currServ.hackDifficulty;
|
|
|
|
post("Server security level: " + (!isHacknet ? numeralWrapper.formatServerSecurity(security) : "N/A"));
|
|
|
|
const hackingChance = calculateHackingChance(currServ, Player)
|
|
|
|
post("Chance to hack: " + (!isHacknet ? numeralWrapper.formatPercentage(hackingChance) : "N/A"));
|
|
|
|
const hackingTime = calculateHackingTime(currServ, Player)*1000;
|
|
|
|
post("Time to hack: " + (!isHacknet ? convertTimeMsToTimeElapsedString(hackingTime, true) : "N/A"));
|
|
|
|
postElement(<>Total money available on server: {!isHacknet ? Money(currServ.moneyAvailable) : "N/A"}</>);
|
|
|
|
const numPort = currServ.numOpenPortsRequired;
|
|
|
|
post("Required number of open ports for NUKE: " + (!isHacknet ? numPort : "N/A"));
|
|
|
|
post("SSH port: "+ (currServ.sshPortOpen ? "Open" : "Closed"))
|
|
|
|
post("FTP port: "+ (currServ.ftpPortOpen ? "Open" : "Closed"))
|
|
|
|
post("SMTP port: "+ (currServ.smtpPortOpen ? "Open" : "Closed"))
|
|
|
|
post("HTTP port: "+ (currServ.httpPortOpen ? "Open" : "Closed"))
|
|
|
|
post("SQL port: "+ (currServ.sqlPortOpen ? "Open" : "Closed"))
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
2016-11-01 06:30:59 +01:00
|
|
|
Terminal.analyzeFlag = false;
|
2017-07-25 03:06:40 +02:00
|
|
|
},
|
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
executeCommands : function(commands) {
|
|
|
|
// Sanitize input
|
|
|
|
commands = commands.trim();
|
2019-04-13 03:22:46 +02:00
|
|
|
commands = commands.replace(/\s\s+/g, ' '); // Replace all extra whitespace in command with a single space
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
// Handle Terminal History - multiple commands should be saved as one
|
|
|
|
if (Terminal.commandHistory[Terminal.commandHistory.length-1] != commands) {
|
|
|
|
Terminal.commandHistory.push(commands);
|
2017-06-06 03:34:00 +02:00
|
|
|
if (Terminal.commandHistory.length > 50) {
|
|
|
|
Terminal.commandHistory.splice(0, 1);
|
|
|
|
}
|
2017-05-01 19:23:20 +02:00
|
|
|
}
|
2017-06-06 23:22:57 +02:00
|
|
|
Terminal.commandHistoryIndex = Terminal.commandHistory.length;
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
// Split commands and execute sequentially
|
2021-05-08 18:44:21 +02:00
|
|
|
commands = commands
|
|
|
|
.match(/(?:'[^']*'|"[^"]*"|[^;"])*/g)
|
|
|
|
.map(substituteAliases)
|
|
|
|
.map(c => c.match(/(?:'[^']*'|"[^"]*"|[^;"])*/g))
|
|
|
|
.flat();
|
2019-02-05 07:45:04 +01:00
|
|
|
for (let i = 0; i < commands.length; i++) {
|
|
|
|
if(commands[i].match(/^\s*$/)) { continue; } // Don't run commands that only have whitespace
|
2019-02-06 08:06:48 +01:00
|
|
|
Terminal.executeCommand(commands[i].trim());
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseCommandArguments : function(command) {
|
|
|
|
// This will be used to keep track of whether we're in a quote. This is for situations
|
|
|
|
// like the alias command:
|
|
|
|
// alias run="run NUKE.exe"
|
|
|
|
// We want the run="run NUKE.exe" to be parsed as a single command, so this flag
|
|
|
|
// will keep track of whether we have a quote in
|
|
|
|
let inQuote = ``;
|
|
|
|
|
|
|
|
// Returns an array with the command and its arguments in each index
|
|
|
|
// Properly handles quotation marks (e.g. `run foo.script "the sun"` will return [run, foo.script, the sun])
|
|
|
|
const args = [];
|
|
|
|
let start = 0, i = 0;
|
|
|
|
let prevChar = ''; // Previous character
|
|
|
|
while (i < command.length) {
|
|
|
|
let escaped = false; // Check for escaped quotation marks
|
|
|
|
if (i >= 1) {
|
|
|
|
prevChar = command.charAt(i - 1);
|
|
|
|
if (prevChar === "\\") { escaped = true; }
|
|
|
|
}
|
|
|
|
|
|
|
|
const c = command.charAt(i);
|
|
|
|
if (c === '"') { // Double quotes
|
|
|
|
if (!escaped && prevChar === " ") {
|
|
|
|
const endQuote = command.indexOf('"', i+1);
|
|
|
|
if (endQuote !== -1 && (endQuote === command.length-1 || command.charAt(endQuote+1) === " ")) {
|
|
|
|
args.push(command.substr(i+1, (endQuote - i - 1)));
|
|
|
|
if (endQuote === command.length-1) {
|
|
|
|
start = i = endQuote+1;
|
|
|
|
} else {
|
2019-04-13 03:22:46 +02:00
|
|
|
start = i = endQuote+2; // Skip the space
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (inQuote === ``) {
|
|
|
|
inQuote = `"`;
|
|
|
|
} else if (inQuote === `"`) {
|
|
|
|
inQuote = ``;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (c === "'") { // Single quotes, same thing as above
|
|
|
|
if (!escaped && prevChar === " ") {
|
|
|
|
const endQuote = command.indexOf("'", i+1);
|
|
|
|
if (endQuote !== -1 && (endQuote === command.length-1 || command.charAt(endQuote+1) === " ")) {
|
|
|
|
args.push(command.substr(i+1, (endQuote - i - 1)));
|
|
|
|
if (endQuote === command.length-1) {
|
|
|
|
start = i = endQuote+1;
|
|
|
|
} else {
|
2019-04-13 03:22:46 +02:00
|
|
|
start = i = endQuote+2; // Skip the space
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (inQuote === ``) {
|
|
|
|
inQuote = `'`;
|
|
|
|
} else if (inQuote === `'`) {
|
|
|
|
inQuote = ``;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (c === " " && inQuote === ``) {
|
|
|
|
let arg = command.substr(start, i-start);
|
|
|
|
|
|
|
|
// If this is a number, convert it from a string to number
|
|
|
|
if (isNumber(arg)) {
|
|
|
|
args.push(parseFloat(arg));
|
|
|
|
} else {
|
|
|
|
args.push(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
start = i+1;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the last argument
|
|
|
|
if (start !== i) {
|
|
|
|
let arg = command.substr(start, i-start);
|
|
|
|
|
|
|
|
// If this is a number, convert it from string to number
|
|
|
|
if (isNumber(arg)) {
|
|
|
|
args.push(parseFloat(arg));
|
|
|
|
} else {
|
|
|
|
args.push(arg);
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 01:23:46 +01:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
return args;
|
|
|
|
},
|
|
|
|
|
2021-04-29 02:07:26 +02:00
|
|
|
executeCommand : function(command) {
|
|
|
|
if (Terminal.hackFlag || Terminal.backdoorFlag || Terminal.analyzeFlag) {
|
2019-07-16 06:40:13 +02:00
|
|
|
postError(`Cannot execute command (${command}) while an action is in progress`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Allow usage of ./
|
2017-07-22 00:54:55 +02:00
|
|
|
if (command.startsWith("./")) {
|
2017-07-27 04:56:14 +02:00
|
|
|
command = "run " + command.slice(2);
|
2017-07-22 00:54:55 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Only split the first space
|
2021-04-18 17:18:56 +02:00
|
|
|
var commandArray = Terminal.parseCommandArguments(command);
|
|
|
|
if (commandArray.length == 0) { return; }
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-05-06 08:24:01 +02:00
|
|
|
/****************** Interactive Tutorial Terminal Commands ******************/
|
2018-08-29 21:06:21 +02:00
|
|
|
if (ITutorial.isRunning) {
|
2017-05-06 08:24:01 +02:00
|
|
|
var foodnstuffServ = GetServerByHostname("foodnstuff");
|
|
|
|
if (foodnstuffServ == null) {throw new Error("Could not get foodnstuff server"); return;}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-08-29 21:06:21 +02:00
|
|
|
switch(ITutorial.currStep) {
|
2017-05-06 08:24:01 +02:00
|
|
|
case iTutorialSteps.TerminalHelp:
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length === 1 && commandArray[0] == "help") {
|
2017-07-22 00:54:55 +02:00
|
|
|
post(TerminalHelpText);
|
2017-05-06 08:24:01 +02:00
|
|
|
iTutorialNextStep();
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
|
|
|
case iTutorialSteps.TerminalLs:
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length === 1 && commandArray[0] == "ls") {
|
2017-05-06 08:24:01 +02:00
|
|
|
Terminal.executeListCommand(commandArray);
|
|
|
|
iTutorialNextStep();
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
|
|
|
case iTutorialSteps.TerminalScan:
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length === 1 && commandArray[0] == "scan") {
|
2017-05-06 08:24:01 +02:00
|
|
|
Terminal.executeScanCommand(commandArray);
|
|
|
|
iTutorialNextStep();
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
2017-05-25 01:23:28 +02:00
|
|
|
case iTutorialSteps.TerminalScanAnalyze1:
|
|
|
|
if (commandArray.length == 1 && commandArray[0] == "scan-analyze") {
|
|
|
|
Terminal.executeScanAnalyzeCommand(1);
|
|
|
|
iTutorialNextStep();
|
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
|
|
|
break;
|
|
|
|
case iTutorialSteps.TerminalScanAnalyze2:
|
|
|
|
if (commandArray.length == 2 && commandArray[0] == "scan-analyze" &&
|
2019-02-05 07:45:04 +01:00
|
|
|
commandArray[1] === 2) {
|
2017-05-25 01:23:28 +02:00
|
|
|
Terminal.executeScanAnalyzeCommand(2);
|
|
|
|
iTutorialNextStep();
|
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
|
|
|
break;
|
2017-05-06 08:24:01 +02:00
|
|
|
case iTutorialSteps.TerminalConnect:
|
|
|
|
if (commandArray.length == 2) {
|
2017-05-25 01:23:28 +02:00
|
|
|
if ((commandArray[0] == "connect") &&
|
2017-05-06 08:24:01 +02:00
|
|
|
(commandArray[1] == "foodnstuff" || commandArray[1] == foodnstuffServ.ip)) {
|
|
|
|
Player.getCurrentServer().isConnectedTo = false;
|
|
|
|
Player.currentServer = foodnstuffServ.ip;
|
|
|
|
Player.getCurrentServer().isConnectedTo = true;
|
|
|
|
post("Connected to foodnstuff");
|
|
|
|
iTutorialNextStep();
|
|
|
|
} else {post("Wrong command! Try again!"); return;}
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
|
|
|
case iTutorialSteps.TerminalAnalyze:
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length === 1 && commandArray[0] === "analyze") {
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
post("Incorrect usage of analyze command. Usage: analyze");
|
|
|
|
return;
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
2018-08-30 19:00:38 +02:00
|
|
|
Terminal.startAnalyze();
|
2017-05-06 08:24:01 +02:00
|
|
|
iTutorialNextStep();
|
|
|
|
} else {
|
2017-05-16 05:31:05 +02:00
|
|
|
post("Bad command. Please follow the tutorial");
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case iTutorialSteps.TerminalNuke:
|
2017-07-25 03:06:40 +02:00
|
|
|
if (commandArray.length == 2 &&
|
2017-05-06 08:24:01 +02:00
|
|
|
commandArray[0] == "run" && commandArray[1] == "NUKE.exe") {
|
|
|
|
foodnstuffServ.hasAdminRights = true;
|
|
|
|
post("NUKE successful! Gained root access to foodnstuff");
|
|
|
|
iTutorialNextStep();
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
|
|
|
case iTutorialSteps.TerminalManualHack:
|
|
|
|
if (commandArray.length == 1 && commandArray[0] == "hack") {
|
2018-08-30 19:00:38 +02:00
|
|
|
Terminal.startHack();
|
2017-05-06 08:24:01 +02:00
|
|
|
iTutorialNextStep();
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
2017-05-06 08:24:01 +02:00
|
|
|
case iTutorialSteps.TerminalCreateScript:
|
2017-07-25 03:06:40 +02:00
|
|
|
if (commandArray.length == 2 &&
|
2017-05-06 08:24:01 +02:00
|
|
|
commandArray[0] == "nano" && commandArray[1] == "foodnstuff.script") {
|
2018-03-12 20:53:07 +01:00
|
|
|
Engine.loadScriptEditorContent("foodnstuff.script", "");
|
2017-05-06 08:24:01 +02:00
|
|
|
iTutorialNextStep();
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2019-02-05 07:45:04 +01:00
|
|
|
break;
|
2017-05-06 08:24:01 +02:00
|
|
|
case iTutorialSteps.TerminalFree:
|
|
|
|
if (commandArray.length == 1 && commandArray[0] == "free") {
|
|
|
|
Terminal.executeFreeCommand(commandArray);
|
|
|
|
iTutorialNextStep();
|
2019-02-05 07:45:04 +01:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
|
|
|
case iTutorialSteps.TerminalRunScript:
|
2017-07-25 03:06:40 +02:00
|
|
|
if (commandArray.length == 2 &&
|
2017-05-06 08:24:01 +02:00
|
|
|
commandArray[0] == "run" && commandArray[1] == "foodnstuff.script") {
|
2019-02-12 01:23:46 +01:00
|
|
|
Terminal.runScript(commandArray);
|
2017-05-06 08:24:01 +02:00
|
|
|
iTutorialNextStep();
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
|
|
|
case iTutorialSteps.ActiveScriptsToTerminal:
|
|
|
|
if (commandArray.length == 2 &&
|
|
|
|
commandArray[0] == "tail" && commandArray[1] == "foodnstuff.script") {
|
2019-04-13 03:22:46 +02:00
|
|
|
// Check that the script exists on this machine
|
2017-06-18 23:01:23 +02:00
|
|
|
var runningScript = findRunningScript("foodnstuff.script", [], Player.getCurrentServer());
|
|
|
|
if (runningScript == null) {
|
|
|
|
post("Error: No such script exists");
|
|
|
|
return;
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
2017-06-18 23:01:23 +02:00
|
|
|
logBoxCreate(runningScript);
|
2017-05-06 08:24:01 +02:00
|
|
|
iTutorialNextStep();
|
2017-05-16 05:31:05 +02:00
|
|
|
} else {post("Bad command. Please follow the tutorial");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
2017-07-25 03:06:40 +02:00
|
|
|
default:
|
2017-05-06 08:24:01 +02:00
|
|
|
post("Please follow the tutorial, or click 'Exit Tutorial' if you'd like to skip it");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-05-06 08:24:01 +02:00
|
|
|
/****************** END INTERACTIVE TUTORIAL ******************/
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-05-06 08:24:01 +02:00
|
|
|
/* Command parser */
|
2017-06-17 04:53:57 +02:00
|
|
|
var s = Player.getCurrentServer();
|
2021-04-18 17:18:56 +02:00
|
|
|
switch (commandArray[0].toLowerCase()) {
|
2017-05-23 05:50:06 +02:00
|
|
|
case "alias":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length === 1) {
|
2017-05-23 05:50:06 +02:00
|
|
|
printAliases();
|
2017-06-30 18:44:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length === 2) {
|
|
|
|
if (parseAliasDeclaration(commandArray[1])) {
|
|
|
|
post(`Set alias ${commandArray[1]}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (commandArray.length === 3) {
|
|
|
|
if (commandArray[1] === "-g") {
|
|
|
|
if (parseAliasDeclaration(commandArray[2], true)) {
|
2021-05-08 18:44:21 +02:00
|
|
|
post(`Set global alias ${commandArray[2]}`);
|
2017-07-05 19:53:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-05-23 15:49:20 +02:00
|
|
|
}
|
2017-05-23 05:50:06 +02:00
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
postError('Incorrect usage of alias command. Usage: alias [-g] [aliasname="value"]');
|
2017-05-23 05:50:06 +02:00
|
|
|
break;
|
2021-04-18 17:18:56 +02:00
|
|
|
case "analyze":
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
post("Incorrect usage of analyze command. Usage: analyze");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
2018-08-30 19:00:38 +02:00
|
|
|
Terminal.startAnalyze();
|
2021-04-29 02:07:26 +02:00
|
|
|
break;
|
|
|
|
case "backdoor":
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
post("Incorrect usage of backdoor command. Usage: backdoor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.purchasedByPlayer) {
|
|
|
|
postError("Cannot use backdoor on your own machines! You are currently connected to your home PC or one of your purchased servers");
|
|
|
|
} else if (!s.hasAdminRights) {
|
|
|
|
postError("You do not have admin rights for this machine! Cannot backdoor");
|
|
|
|
} else if (s.requiredHackingSkill > Player.hacking_skill) {
|
|
|
|
postError("Your hacking skill is not high enough to use backdoor on this machine. Try analyzing the machine to determine the required hacking skill");
|
|
|
|
} else if (s instanceof HacknetServer) {
|
|
|
|
postError("Cannot use backdoor on this type of Server")
|
|
|
|
} else {
|
|
|
|
Terminal.startBackdoor();
|
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
2017-05-05 17:50:55 +02:00
|
|
|
case "buy":
|
2017-06-14 03:07:02 +02:00
|
|
|
if (SpecialServerIps.hasOwnProperty("Darkweb Server")) {
|
|
|
|
executeDarkwebTerminalCommand(commandArray);
|
2017-06-15 03:27:22 +02:00
|
|
|
} else {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("You need to be able to connect to the Dark Web to use the buy command. (Maybe there's a TOR router you can buy somewhere)");
|
2017-06-14 03:07:02 +02:00
|
|
|
}
|
2017-06-02 06:15:45 +02:00
|
|
|
break;
|
2019-04-05 11:08:41 +02:00
|
|
|
case "cat": {
|
2019-04-10 08:07:12 +02:00
|
|
|
try {
|
|
|
|
if (commandArray.length !== 2) {
|
|
|
|
postError("Incorrect usage of cat command. Usage: cat [file]");
|
2017-06-02 06:15:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
const filename = Terminal.getFilepath(commandArray[1]);
|
2021-04-18 17:18:56 +02:00
|
|
|
if (!filename.endsWith(".msg") && !filename.endsWith(".lit") && !filename.endsWith(".txt")) {
|
|
|
|
postError("Only .msg, .txt, and .lit files are viewable with cat (filename must end with .msg, .txt, or .lit)");
|
2017-10-12 04:00:22 +02:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
if (filename.endsWith(".msg") || filename.endsWith(".lit")) {
|
|
|
|
for (let i = 0; i < s.messages.length; ++i) {
|
|
|
|
if (filename.endsWith(".lit") && s.messages[i] === filename) {
|
|
|
|
showLiterature(s.messages[i]);
|
|
|
|
return;
|
|
|
|
} else if (filename.endsWith(".msg") && s.messages[i].filename === filename) {
|
|
|
|
showMessage(s.messages[i]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (filename.endsWith(".txt")) {
|
|
|
|
const txt = Terminal.getTextFile(filename);
|
|
|
|
if (txt != null) {
|
|
|
|
txt.show();
|
|
|
|
return;
|
|
|
|
}
|
2017-10-12 04:00:22 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
postError(`No such file ${filename}`);
|
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
2017-10-12 04:00:22 +02:00
|
|
|
}
|
2017-06-15 03:19:52 +02:00
|
|
|
break;
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
case "cd": {
|
2021-04-29 02:07:26 +02:00
|
|
|
if (commandArray.length > 2) {
|
2019-04-05 11:08:41 +02:00
|
|
|
postError("Incorrect number of arguments. Usage: cd [dir]");
|
|
|
|
} else {
|
2021-04-29 02:07:26 +02:00
|
|
|
let dir = commandArray.length === 2 ? commandArray[1] : "/";
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
let evaledDir;
|
|
|
|
if (dir === "/") {
|
|
|
|
evaledDir = "/";
|
2019-04-05 11:08:41 +02:00
|
|
|
} else {
|
2019-04-10 08:07:12 +02:00
|
|
|
// Ignore trailing slashes
|
|
|
|
dir = removeTrailingSlash(dir);
|
2019-04-05 11:08:41 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
evaledDir = evaluateDirectoryPath(dir, Terminal.currDir);
|
|
|
|
if (evaledDir == null || evaledDir === "") {
|
|
|
|
postError("Invalid path. Failed to change directories");
|
|
|
|
return;
|
|
|
|
}
|
2021-04-29 02:07:26 +02:00
|
|
|
|
|
|
|
const server = Player.getCurrentServer();
|
2021-05-06 01:09:14 +02:00
|
|
|
if(!server.scripts.some(script => script.filename.startsWith(evaledDir)) &&
|
|
|
|
!server.textFiles.some(file => file.fn.startsWith(evaledDir))) {
|
2021-04-29 02:07:26 +02:00
|
|
|
postError("Invalid path. Failed to change directories");
|
|
|
|
return;
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
Terminal.currDir = evaledDir;
|
|
|
|
|
|
|
|
// Reset input to update current directory on UI
|
|
|
|
Terminal.resetTerminalInput();
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
case "check": {
|
|
|
|
try {
|
|
|
|
if (commandArray.length < 2) {
|
|
|
|
postError("Incorrect number of arguments. Usage: check [script] [arg1] [arg2]...");
|
|
|
|
} else {
|
|
|
|
const scriptName = Terminal.getFilepath(commandArray[1]);
|
2019-04-13 03:22:46 +02:00
|
|
|
// Can only tail script files
|
2019-04-10 08:07:12 +02:00
|
|
|
if (!isScriptFilename(scriptName)) {
|
|
|
|
postError("tail can only be called on .script files (filename must end with .script)");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Get args
|
|
|
|
let args = [];
|
|
|
|
for (var i = 2; i < commandArray.length; ++i) {
|
|
|
|
args.push(commandArray[i]);
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Check that the script exists on this machine
|
|
|
|
var runningScript = findRunningScript(scriptName, args, s);
|
|
|
|
if (runningScript == null) {
|
|
|
|
postError("No such script exists");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
runningScript.displayLog();
|
2017-06-15 03:19:52 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
2017-06-15 03:19:52 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2017-05-05 17:50:55 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
case "clear":
|
|
|
|
case "cls":
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of clear/cls command. Usage: clear/cls");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
$("#terminal tr:not(:last)").remove();
|
|
|
|
postNetburnerText();
|
|
|
|
break;
|
|
|
|
case "connect": {
|
|
|
|
// Disconnect from current server in terminal and connect to new one
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 2) {
|
|
|
|
postError("Incorrect usage of connect command. Usage: connect [ip/hostname]");
|
2016-10-24 08:34:04 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
let ip = commandArray[1];
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
for (let i = 0; i < s.serversOnNetwork.length; i++) {
|
2019-03-05 02:40:28 +01:00
|
|
|
if (getServerOnNetwork(s, i).ip == ip || getServerOnNetwork(s, i).hostname == ip) {
|
2017-06-23 16:23:35 +02:00
|
|
|
Terminal.connectToServer(ip);
|
2016-10-24 08:34:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Host not found");
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "download": {
|
|
|
|
try {
|
|
|
|
if (commandArray.length !== 2) {
|
|
|
|
postError("Incorrect usage of download command. Usage: download [script/text file]");
|
|
|
|
return;
|
2018-03-12 20:39:04 +01:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
const fn = commandArray[1];
|
|
|
|
if (fn === "*" || fn === "*.script" || fn === "*.txt") {
|
2019-04-13 03:22:46 +02:00
|
|
|
// Download all scripts as a zip
|
2019-04-10 08:07:12 +02:00
|
|
|
var zip = new JSZip();
|
|
|
|
if (fn === "*" || fn === "*.script") {
|
|
|
|
for (var i = 0; i < s.scripts.length; ++i) {
|
|
|
|
var file = new Blob([s.scripts[i].code], {type:"text/plain"});
|
|
|
|
zip.file(s.scripts[i].filename + ".js", file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fn === "*" || fn === "*.txt") {
|
|
|
|
for (var i = 0; i < s.textFiles.length; ++i) {
|
|
|
|
var file = new Blob([s.textFiles[i].text], {type:"text/plain"});
|
|
|
|
zip.file(s.textFiles[i].fn, file);
|
|
|
|
}
|
2018-03-12 20:39:04 +01:00
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
let zipFn;
|
|
|
|
switch (fn) {
|
|
|
|
case "*.script":
|
|
|
|
zipFn = "bitburnerScripts.zip"; break;
|
|
|
|
case "*.txt":
|
|
|
|
zipFn = "bitburnerTexts.zip"; break;
|
|
|
|
default:
|
|
|
|
zipFn = "bitburnerFiles.zip"; break;
|
|
|
|
}
|
2018-03-12 20:39:04 +01:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
zip.generateAsync({type:"blob"}).then(function(content) {
|
|
|
|
FileSaver.saveAs(content, zipFn);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (isScriptFilename(fn)) {
|
|
|
|
// Download a single script
|
|
|
|
const script = Terminal.getScript(fn);
|
|
|
|
if (script != null) {
|
|
|
|
return script.download();
|
2018-02-15 05:26:43 +01:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
} else if (fn.endsWith(".txt")) {
|
|
|
|
// Download a single text file
|
|
|
|
const txt = Terminal.getTextFile(fn);
|
|
|
|
if (txt != null) {
|
|
|
|
return txt.download();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
postError(`Cannot download this filetype`);
|
|
|
|
return;
|
2018-02-15 05:26:43 +01:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
postError(`${fn} does not exist`);
|
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
2017-10-12 04:00:22 +02:00
|
|
|
}
|
2018-11-18 01:23:48 +01:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "expr": {
|
2018-11-18 01:23:48 +01:00
|
|
|
if (commandArray.length <= 1) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Incorrect usage of expr command. Usage: expr [math expression]");
|
2018-11-18 01:23:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
let expr = commandArray.slice(1).join("");
|
2018-11-18 01:23:48 +01:00
|
|
|
|
|
|
|
// Sanitize the math expression
|
2019-02-05 07:45:04 +01:00
|
|
|
let sanitizedExpr = expr.replace(/s+/g, '').replace(/[^-()\d/*+.]/g, '');
|
2018-11-18 01:23:48 +01:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = eval(sanitizedExpr);
|
|
|
|
} catch(e) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError(`Could not evaluate expression: ${sanitizedExpr}`);
|
2018-11-18 01:23:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
post(result);
|
2017-10-12 04:00:22 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
case "free":
|
|
|
|
Terminal.executeFreeCommand(commandArray);
|
|
|
|
break;
|
|
|
|
case "hack": {
|
2019-03-30 00:14:32 +01:00
|
|
|
if (commandArray.length !== 1) {
|
2021-04-18 17:18:56 +02:00
|
|
|
postError("Incorrect usage of hack command. Usage: hack");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
// Hack the current PC (usually for money)
|
|
|
|
// You can't hack your home pc or servers you purchased
|
|
|
|
if (s.purchasedByPlayer) {
|
|
|
|
postError("Cannot hack your own machines! You are currently connected to your home PC or one of your purchased servers");
|
|
|
|
} else if (s.hasAdminRights == false ) {
|
|
|
|
postError("You do not have admin rights for this machine! Cannot hack");
|
|
|
|
} else if (s.requiredHackingSkill > Player.hacking_skill) {
|
|
|
|
postError("Your hacking skill is not high enough to attempt hacking this machine. Try analyzing the machine to determine the required hacking skill");
|
|
|
|
} else if (s instanceof HacknetServer) {
|
2019-03-30 00:14:32 +01:00
|
|
|
postError("Cannot hack this type of Server")
|
|
|
|
} else {
|
2018-08-30 19:00:38 +02:00
|
|
|
Terminal.startHack();
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-03-30 00:14:32 +01:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
case "help":
|
|
|
|
if (commandArray.length !== 1 && commandArray.length !== 2) {
|
|
|
|
postError("Incorrect usage of help command. Usage: help");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
if (commandArray.length === 1) {
|
2017-07-22 00:54:55 +02:00
|
|
|
post(TerminalHelpText);
|
|
|
|
} else {
|
|
|
|
var cmd = commandArray[1];
|
|
|
|
var txt = HelpTexts[cmd];
|
|
|
|
if (txt == null) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("No help topics match '" + cmd + "'");
|
2017-07-22 00:54:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
post(txt);
|
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
|
|
|
case "home":
|
|
|
|
if (commandArray.length !== 1) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Incorrect usage of home command. Usage: home");
|
|
|
|
return;
|
2017-05-05 03:08:44 +02:00
|
|
|
}
|
|
|
|
Player.getCurrentServer().isConnectedTo = false;
|
|
|
|
Player.currentServer = Player.getHomeComputer().ip;
|
|
|
|
Player.getCurrentServer().isConnectedTo = true;
|
|
|
|
post("Connected to home");
|
2019-04-10 08:07:12 +02:00
|
|
|
Terminal.currDir = "/";
|
2017-09-13 16:22:22 +02:00
|
|
|
Terminal.resetTerminalInput();
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
|
|
|
case "hostname":
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of hostname command. Usage: hostname");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
post(Player.getCurrentServer().hostname);
|
|
|
|
break;
|
|
|
|
case "ifconfig":
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of ifconfig command. Usage: ifconfig");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
post(Player.getCurrentServer().ip);
|
|
|
|
break;
|
|
|
|
case "kill": {
|
2019-02-05 07:45:04 +01:00
|
|
|
Terminal.executeKillCommand(commandArray);
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "killall": {
|
|
|
|
for (let i = s.runningScripts.length - 1; i >= 0; --i) {
|
2019-06-27 09:01:06 +02:00
|
|
|
killWorkerScript(s.runningScripts[i], s.ip, false);
|
2017-06-05 19:59:30 +02:00
|
|
|
}
|
2019-06-27 09:01:06 +02:00
|
|
|
WorkerScriptStartStopEventEmitter.emitEvent();
|
2019-07-12 04:37:17 +02:00
|
|
|
post("Killing all running scripts");
|
2017-06-05 19:59:30 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
case "ls": {
|
2017-05-06 08:24:01 +02:00
|
|
|
Terminal.executeListCommand(commandArray);
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "lscpu": {
|
2017-10-10 06:56:48 +02:00
|
|
|
post(Player.getCurrentServer().cpuCores + " Core(s)");
|
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "mem": {
|
2019-02-05 07:45:04 +01:00
|
|
|
Terminal.executeMemCommand(commandArray);
|
2017-05-15 04:46:52 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "mv": {
|
|
|
|
if (commandArray.length !== 3) {
|
|
|
|
postError(`Incorrect number of arguments. Usage: mv [src] [dest]`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const source = commandArray[1];
|
|
|
|
const dest = commandArray[2];
|
|
|
|
|
2019-04-10 08:34:23 +02:00
|
|
|
if (!isScriptFilename(source) && !source.endsWith(".txt")) {
|
|
|
|
postError(`'mv' can only be used on scripts and text files (.txt)`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
const srcFile = Terminal.getFile(source);
|
|
|
|
if (srcFile == null) {
|
|
|
|
postError(`Source file ${source} does not exist`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourcePath = Terminal.getFilepath(source);
|
|
|
|
const destPath = Terminal.getFilepath(dest);
|
|
|
|
|
|
|
|
const destFile = Terminal.getFile(dest);
|
|
|
|
|
|
|
|
// 'mv' command only works on scripts and txt files.
|
|
|
|
// Also, you can't convert between different file types
|
|
|
|
if (isScriptFilename(source)) {
|
|
|
|
if (!isScriptFilename(dest)) {
|
|
|
|
postError(`Source and destination files must have the same type`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command doesnt work if script is running
|
|
|
|
if (s.isRunning(sourcePath)) {
|
|
|
|
postError(`Cannot use 'mv' on a script that is running`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destFile != null) {
|
|
|
|
// Already exists, will be overwritten, so we'll delete it
|
|
|
|
const status = s.removeFile(destPath);
|
|
|
|
if (!status.res) {
|
|
|
|
postError(`Something went wrong...please contact game dev (probably a bug)`);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
post("Warning: The destination file was overwritten");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srcFile.filename = destPath;
|
|
|
|
} else if (source.endsWith(".txt")) {
|
|
|
|
if (!dest.endsWith(".txt")) {
|
|
|
|
postError(`Source and destination files must have the same type`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destFile != null) {
|
|
|
|
// Already exists, will be overwritten, so we'll delete it
|
|
|
|
const status = s.removeFile(destPath);
|
|
|
|
if (!status.res) {
|
|
|
|
postError(`Something went wrong...please contact game dev (probably a bug)`);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
post("Warning: The destination file was overwritten");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srcFile.fn = destPath;
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
case "nano":
|
2019-02-05 07:45:04 +01:00
|
|
|
Terminal.executeNanoCommand(commandArray);
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
|
|
|
case "ps":
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of ps command. Usage: ps");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
for (let i = 0; i < s.runningScripts.length; i++) {
|
2019-02-05 07:45:04 +01:00
|
|
|
let rsObj = s.runningScripts[i];
|
2019-06-27 09:01:06 +02:00
|
|
|
let res = `(PID - ${rsObj.pid}) ${rsObj.filename}`;
|
2019-02-05 07:45:04 +01:00
|
|
|
for (let j = 0; j < rsObj.args.length; ++j) {
|
2017-06-17 04:53:57 +02:00
|
|
|
res += (" " + rsObj.args[j].toString());
|
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
post(res);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "rm": {
|
|
|
|
if (commandArray.length !== 2) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Incorrect number of arguments. Usage: rm [program/script]");
|
|
|
|
return;
|
2017-04-13 21:36:03 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Check programs
|
2021-05-18 00:09:47 +02:00
|
|
|
let delTarget, status;
|
|
|
|
try {
|
|
|
|
delTarget = Terminal.getFilepath(commandArray[1]);
|
|
|
|
status = s.removeFile(delTarget);
|
|
|
|
} catch(err) {
|
|
|
|
status = {
|
|
|
|
res: false,
|
|
|
|
msg: 'No such file exists'
|
|
|
|
};
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (!status.res) {
|
|
|
|
postError(status.msg);
|
2017-09-12 01:14:51 +02:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
case "run":
|
|
|
|
// Run a program or a script
|
|
|
|
if (commandArray.length < 2) {
|
|
|
|
postError("Incorrect number of arguments. Usage: run [program/script] [-t] [num threads] [arg1] [arg2]...");
|
|
|
|
} else {
|
|
|
|
var executableName = commandArray[1];
|
2018-01-09 21:48:06 +01:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Secret Music player!
|
2018-01-09 21:48:06 +01:00
|
|
|
if (executableName === "musicplayer") {
|
|
|
|
post('<iframe src="https://open.spotify.com/embed/user/danielyxie/playlist/1ORnnL6YNvXOracUaUV2kh" width="300" height="380" frameborder="0" allowtransparency="true"></iframe>', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// Check if its a script or just a program/executable
|
2019-02-06 03:32:15 +01:00
|
|
|
if (isScriptFilename(executableName)) {
|
2021-04-18 17:18:56 +02:00
|
|
|
Terminal.runScript(commandArray);
|
2018-09-14 23:03:31 +02:00
|
|
|
} else if (executableName.endsWith(".cct")) {
|
|
|
|
Terminal.runContract(executableName);
|
2021-04-18 17:18:56 +02:00
|
|
|
} else {
|
2019-02-20 09:42:27 +01:00
|
|
|
Terminal.runProgram(commandArray);
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-05-23 19:36:35 +02:00
|
|
|
case "scan":
|
|
|
|
Terminal.executeScanCommand(commandArray);
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
2017-05-23 18:15:17 +02:00
|
|
|
case "scan-analyze":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length === 1) {
|
2017-05-24 19:07:33 +02:00
|
|
|
Terminal.executeScanAnalyzeCommand(1);
|
2019-02-05 07:45:04 +01:00
|
|
|
} else {
|
|
|
|
// # of args must be 2 or 3
|
|
|
|
if (commandArray.length > 3) {
|
|
|
|
postError("Incorrect usage of scan-analyze command. usage: scan-analyze [depth]");
|
|
|
|
return;
|
2017-09-19 20:38:03 +02:00
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
let all = false;
|
|
|
|
if (commandArray.length === 3 && commandArray[2] === "-a") {
|
|
|
|
all = true;
|
2017-09-19 20:38:03 +02:00
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
|
|
|
|
let depth = parseInt(commandArray[1]);
|
|
|
|
|
2017-05-24 23:35:24 +02:00
|
|
|
if (isNaN(depth) || depth < 0) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Incorrect usage of scan-analyze command. depth argument must be positive numeric");
|
2017-05-24 23:35:24 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-06-02 01:31:34 +02:00
|
|
|
if (depth > 3 && !Player.hasProgram(Programs.DeepscanV1.name) &&
|
|
|
|
!Player.hasProgram(Programs.DeepscanV2.name)) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("You cannot scan-analyze with that high of a depth. Maximum depth is 3");
|
2017-05-24 23:35:24 +02:00
|
|
|
return;
|
2018-06-02 01:31:34 +02:00
|
|
|
} else if (depth > 5 && !Player.hasProgram(Programs.DeepscanV2.name)) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("You cannot scan-analyze with that high of a depth. Maximum depth is 5");
|
2017-05-24 23:35:24 +02:00
|
|
|
return;
|
|
|
|
} else if (depth > 10) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("You cannot scan-analyze with that high of a depth. Maximum depth is 10");
|
2017-05-24 19:07:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-19 20:38:03 +02:00
|
|
|
Terminal.executeScanAnalyzeCommand(depth, all);
|
2017-07-25 03:06:40 +02:00
|
|
|
}
|
2017-05-23 18:15:17 +02:00
|
|
|
break;
|
2018-09-10 16:59:07 +02:00
|
|
|
/* eslint-disable no-case-declarations */
|
2021-04-18 17:18:56 +02:00
|
|
|
case "scp":
|
2019-02-05 07:45:04 +01:00
|
|
|
Terminal.executeScpCommand(commandArray);
|
2018-09-08 03:53:11 +02:00
|
|
|
break;
|
2018-09-10 16:59:07 +02:00
|
|
|
/* eslint-enable no-case-declarations */
|
2017-05-06 08:24:01 +02:00
|
|
|
case "sudov":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect number of arguments. Usage: sudov");
|
|
|
|
return;
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
if (s.hasAdminRights) {
|
2017-05-06 08:24:01 +02:00
|
|
|
post("You have ROOT access to this machine");
|
|
|
|
} else {
|
|
|
|
post("You do NOT have root access to this machine");
|
|
|
|
}
|
|
|
|
break;
|
2021-04-18 17:18:56 +02:00
|
|
|
case "tail": {
|
2019-04-10 08:07:12 +02:00
|
|
|
try {
|
|
|
|
if (commandArray.length < 2) {
|
|
|
|
postError("Incorrect number of arguments. Usage: tail [script] [arg1] [arg2]...");
|
2021-05-03 17:58:11 +02:00
|
|
|
} else if(typeof commandArray[1] === 'string') {
|
|
|
|
const scriptName = Terminal.getFilepath(commandArray[1]);
|
|
|
|
if (!isScriptFilename(scriptName)) {
|
|
|
|
postError("tail can only be called on .script, .ns, .js files, or by pid");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-05-03 17:58:11 +02:00
|
|
|
// Get script arguments
|
|
|
|
const args = [];
|
|
|
|
for (let i = 2; i < commandArray.length; ++i) {
|
|
|
|
args.push(commandArray[i]);
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-05-03 17:58:11 +02:00
|
|
|
// Check that the script exists on this machine
|
|
|
|
const runningScript = findRunningScript(scriptName, args, s);
|
|
|
|
if (runningScript == null) {
|
|
|
|
postError("No such script exists");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logBoxCreate(runningScript);
|
|
|
|
} else {
|
|
|
|
const runningScript = findRunningScriptByPid(commandArray[1], Player.getCurrentServer());
|
|
|
|
if (runningScript == null) {
|
|
|
|
postError("No such script exists");
|
|
|
|
return;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-05-03 17:58:11 +02:00
|
|
|
logBoxCreate(runningScript);
|
2017-03-31 23:47:06 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
2017-03-31 23:47:06 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "theme": {
|
2019-02-05 07:45:04 +01:00
|
|
|
let args = commandArray.slice(1);
|
|
|
|
if (args.length !== 1 && args.length !== 3) {
|
|
|
|
postError("Incorrect number of arguments.");
|
|
|
|
postError("Usage: theme [default|muted|solarized] | #[background color hex] #[text color hex] #[highlight color hex]");
|
|
|
|
} else if(args.length === 1){
|
2017-06-07 03:23:51 +02:00
|
|
|
var themeName = args[0];
|
2018-02-15 05:26:43 +01:00
|
|
|
if (themeName == "default"){
|
2017-06-07 03:23:51 +02:00
|
|
|
document.body.style.setProperty('--my-highlight-color',"#ffffff");
|
|
|
|
document.body.style.setProperty('--my-font-color',"#66ff33");
|
|
|
|
document.body.style.setProperty('--my-background-color',"#000000");
|
2018-09-21 21:47:33 +02:00
|
|
|
document.body.style.setProperty('--my-prompt-color', "#f92672");
|
2018-02-15 05:26:43 +01:00
|
|
|
} else if (themeName == "muted"){
|
2017-06-07 03:23:51 +02:00
|
|
|
document.body.style.setProperty('--my-highlight-color',"#ffffff");
|
|
|
|
document.body.style.setProperty('--my-font-color',"#66ff33");
|
|
|
|
document.body.style.setProperty('--my-background-color',"#252527");
|
2018-02-15 05:26:43 +01:00
|
|
|
} else if (themeName == "solarized"){
|
2017-06-07 03:23:51 +02:00
|
|
|
document.body.style.setProperty('--my-highlight-color',"#6c71c4");
|
|
|
|
document.body.style.setProperty('--my-font-color',"#839496");
|
|
|
|
document.body.style.setProperty('--my-background-color',"#002b36");
|
2018-02-15 05:26:43 +01:00
|
|
|
} else {
|
2019-02-05 07:45:04 +01:00
|
|
|
return postError("Theme not found");
|
2017-07-25 03:06:40 +02:00
|
|
|
}
|
2018-09-21 21:47:33 +02:00
|
|
|
FconfSettings.THEME_HIGHLIGHT_COLOR = document.body.style.getPropertyValue("--my-highlight-color");
|
|
|
|
FconfSettings.THEME_FONT_COLOR = document.body.style.getPropertyValue("--my-font-color");
|
|
|
|
FconfSettings.THEME_BACKGROUND_COLOR = document.body.style.getPropertyValue("--my-background-color");
|
|
|
|
FconfSettings.THEME_PROMPT_COLOR = document.body.style.getPropertyValue("--my-prompt-color");
|
2018-02-15 05:26:43 +01:00
|
|
|
} else {
|
2018-01-09 21:48:06 +01:00
|
|
|
var inputBackgroundHex = args[0];
|
|
|
|
var inputTextHex = args[1];
|
|
|
|
var inputHighlightHex = args[2];
|
2018-02-15 05:26:43 +01:00
|
|
|
if (/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(inputBackgroundHex) &&
|
2017-06-07 03:23:51 +02:00
|
|
|
/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(inputTextHex) &&
|
|
|
|
/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(inputHighlightHex)){
|
|
|
|
document.body.style.setProperty('--my-highlight-color',inputHighlightHex);
|
|
|
|
document.body.style.setProperty('--my-font-color',inputTextHex);
|
|
|
|
document.body.style.setProperty('--my-background-color',inputBackgroundHex);
|
2018-09-21 21:47:33 +02:00
|
|
|
FconfSettings.THEME_HIGHLIGHT_COLOR = document.body.style.getPropertyValue("--my-highlight-color");
|
|
|
|
FconfSettings.THEME_FONT_COLOR = document.body.style.getPropertyValue("--my-font-color");
|
|
|
|
FconfSettings.THEME_BACKGROUND_COLOR = document.body.style.getPropertyValue("--my-background-color");
|
2018-02-15 05:26:43 +01:00
|
|
|
} else {
|
2019-02-05 07:45:04 +01:00
|
|
|
return postError("Invalid Hex Input for theme");
|
2017-07-25 03:06:40 +02:00
|
|
|
}
|
2017-06-07 03:23:51 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
case "top": {
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of top command. Usage: top");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
2017-06-16 19:23:42 +02:00
|
|
|
|
2019-06-27 09:01:06 +02:00
|
|
|
// Headers
|
|
|
|
const scriptWidth = 40;
|
|
|
|
const pidWidth = 10;
|
|
|
|
const threadsWidth = 16;
|
|
|
|
|
|
|
|
const scriptTxt = "Script";
|
|
|
|
const pidTxt = "PID";
|
|
|
|
const threadsTxt = "Threads";
|
|
|
|
const ramTxt = "RAM Usage";
|
|
|
|
|
|
|
|
const spacesAfterScriptTxt = " ".repeat(scriptWidth - scriptTxt.length);
|
|
|
|
const spacesAfterPidTxt = " ".repeat(pidWidth - pidTxt.length);
|
|
|
|
const spacesAfterThreadsTxt = " ".repeat(threadsWidth - threadsTxt.length);
|
|
|
|
|
|
|
|
const headers = `${scriptTxt}${spacesAfterScriptTxt}${pidTxt}${spacesAfterPidTxt}${threadsTxt}${spacesAfterThreadsTxt}${ramTxt}`;
|
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
post(headers);
|
2017-06-16 19:23:42 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
let currRunningScripts = s.runningScripts;
|
|
|
|
// Iterate through scripts on current server
|
|
|
|
for (let i = 0; i < currRunningScripts.length; i++) {
|
|
|
|
let script = currRunningScripts[i];
|
2017-06-16 16:57:03 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// Calculate name padding
|
|
|
|
const numSpacesScript = Math.max(0, scriptWidth - script.filename.length);
|
2019-06-27 09:01:06 +02:00
|
|
|
const spacesScript = " ".repeat(numSpacesScript);
|
|
|
|
|
|
|
|
// Calculate PID padding
|
|
|
|
const numSpacesPid = Math.max(0, pidWidth - (script.pid + "").length);
|
|
|
|
const spacesPid = " ".repeat(numSpacesPid);
|
2017-06-16 16:57:03 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// Calculate thread padding
|
|
|
|
const numSpacesThread = Math.max(0, threadsWidth - (script.threads + "").length);
|
2019-06-27 09:01:06 +02:00
|
|
|
const spacesThread = " ".repeat(numSpacesThread);
|
2017-06-16 16:57:03 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// Calculate and transform RAM usage
|
|
|
|
const ramUsage = numeralWrapper.formatRAM(getRamUsageFromRunningScript(script) * script.threads);
|
2019-06-27 09:01:06 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
const entry = [
|
2019-06-27 09:01:06 +02:00
|
|
|
script.filename,
|
|
|
|
spacesScript,
|
|
|
|
script.pid,
|
|
|
|
spacesPid,
|
|
|
|
script.threads,
|
|
|
|
spacesThread,
|
2021-04-30 05:52:56 +02:00
|
|
|
ramUsage,
|
2019-06-27 09:01:06 +02:00
|
|
|
].join("");
|
2021-04-18 17:18:56 +02:00
|
|
|
post(entry);
|
|
|
|
}
|
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "unalias": {
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 2) {
|
|
|
|
postError('Incorrect usage of unalias name. Usage: unalias [alias]');
|
2017-06-21 19:12:08 +02:00
|
|
|
return;
|
|
|
|
} else {
|
2019-02-05 07:45:04 +01:00
|
|
|
if (removeAlias(commandArray[1])) {
|
|
|
|
post(`Removed alias ${commandArray[1]}`);
|
2017-06-21 19:12:08 +02:00
|
|
|
} else {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError(`No such alias exists: ${commandArray[1]}`);
|
2017-06-21 19:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 05:24:38 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
|
|
|
case "wget": {
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 3) {
|
|
|
|
postError("Incorrect usage of wget command. Usage: wget [url] [target file]");
|
|
|
|
return;
|
2018-08-29 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
let url = commandArray[1];
|
2019-05-12 04:20:20 +02:00
|
|
|
let target = Terminal.getFilepath(commandArray[2]);
|
2018-09-08 03:53:11 +02:00
|
|
|
if (!isScriptFilename(target) && !target.endsWith(".txt")) {
|
|
|
|
return post(`wget failed: Invalid target file. Target file must be script or text file`);
|
|
|
|
}
|
2018-08-29 05:24:38 +02:00
|
|
|
$.get(url, function(data) {
|
2018-09-08 03:53:11 +02:00
|
|
|
let res;
|
|
|
|
if (isScriptFilename(target)) {
|
2018-09-27 23:49:23 +02:00
|
|
|
res = s.writeToScriptFile(target, data);
|
2018-09-08 03:53:11 +02:00
|
|
|
} else {
|
2018-09-27 23:49:23 +02:00
|
|
|
res = s.writeToTextFile(target, data);
|
2018-09-08 03:53:11 +02:00
|
|
|
}
|
|
|
|
if (!res.success) {
|
|
|
|
return post("wget failed");
|
|
|
|
}
|
|
|
|
if (res.overwritten) {
|
|
|
|
return post(`wget successfully retrieved content and overwrote ${target}`);
|
|
|
|
}
|
|
|
|
return post(`wget successfully retrieved content to new file ${target}`);
|
2018-08-29 05:24:38 +02:00
|
|
|
}, 'text').fail(function(e) {
|
2019-02-05 07:45:04 +01:00
|
|
|
return postError("wget failed: " + JSON.stringify(e));
|
2018-08-29 05:24:38 +02:00
|
|
|
})
|
2017-06-21 19:12:08 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
default:
|
|
|
|
postError(`Command ${commandArray[0]} not found`);
|
|
|
|
}
|
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-06-23 16:23:35 +02:00
|
|
|
connectToServer: function(ip) {
|
|
|
|
var serv = getServer(ip);
|
|
|
|
if (serv == null) {
|
|
|
|
post("Invalid server. Connection failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Player.getCurrentServer().isConnectedTo = false;
|
|
|
|
Player.currentServer = serv.ip;
|
|
|
|
Player.getCurrentServer().isConnectedTo = true;
|
|
|
|
post("Connected to " + serv.hostname);
|
2019-04-10 08:07:12 +02:00
|
|
|
Terminal.currDir = "/";
|
2017-06-23 16:23:35 +02:00
|
|
|
if (Player.getCurrentServer().hostname == "darkweb") {
|
2019-04-13 03:22:46 +02:00
|
|
|
checkIfConnectedToDarkweb(); // Posts a 'help' message if connecting to dark web
|
2017-06-23 16:23:35 +02:00
|
|
|
}
|
2017-09-13 16:22:22 +02:00
|
|
|
Terminal.resetTerminalInput();
|
2017-06-23 16:23:35 +02:00
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
executeFreeCommand: function(commandArray) {
|
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of free command. Usage: free");
|
|
|
|
return;
|
|
|
|
}
|
2021-03-31 06:45:21 +02:00
|
|
|
const ram = numeralWrapper.formatRAM(Player.getCurrentServer().maxRam);
|
|
|
|
const used = numeralWrapper.formatRAM(Player.getCurrentServer().ramUsed);
|
|
|
|
const avail = numeralWrapper.formatRAM(Player.getCurrentServer().maxRam - Player.getCurrentServer().ramUsed);
|
2019-03-29 17:26:25 +01:00
|
|
|
const maxLength = Math.max(ram.length, Math.max(used.length, avail.length));
|
2021-03-31 06:45:21 +02:00
|
|
|
const usedPercent = numeralWrapper.formatPercentage(Player.getCurrentServer().ramUsed/Player.getCurrentServer().maxRam);
|
2019-03-29 17:26:25 +01:00
|
|
|
|
2021-03-31 06:45:21 +02:00
|
|
|
post(`Total: ${" ".repeat(maxLength-ram.length)}${ram}`);
|
|
|
|
post(`Used: ${" ".repeat(maxLength-used.length)}${used} (${usedPercent})`);
|
|
|
|
post(`Available: ${" ".repeat(maxLength-avail.length)}${avail}`);
|
2019-02-05 07:45:04 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
executeKillCommand: function(commandArray) {
|
2019-04-10 08:07:12 +02:00
|
|
|
try {
|
|
|
|
if (commandArray.length < 2) {
|
|
|
|
postError("Incorrect usage of kill command. Usage: kill [scriptname] [arg1] [arg2]...");
|
|
|
|
return;
|
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
|
2019-07-12 04:37:17 +02:00
|
|
|
// Kill by PID
|
|
|
|
if (typeof commandArray[1] === "number") {
|
|
|
|
const pid = commandArray[1];
|
|
|
|
const res = killWorkerScript(pid);
|
|
|
|
if (res) {
|
|
|
|
post(`Killing script with PID ${pid}`);
|
|
|
|
} else {
|
|
|
|
post(`Failed to kill script with PID ${pid}. No such script exists`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
const s = Player.getCurrentServer();
|
|
|
|
const scriptName = Terminal.getFilepath(commandArray[1]);
|
|
|
|
const args = [];
|
|
|
|
for (let i = 2; i < commandArray.length; ++i) {
|
|
|
|
args.push(commandArray[i]);
|
|
|
|
}
|
2019-07-12 04:37:17 +02:00
|
|
|
const runningScript = s.getRunningScript(scriptName, args);
|
2019-04-10 08:07:12 +02:00
|
|
|
if (runningScript == null) {
|
|
|
|
postError("No such script is running. Nothing to kill");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
killWorkerScript(runningScript, s.ip);
|
2019-07-12 04:37:17 +02:00
|
|
|
post(`Killing ${scriptName}`);
|
2019-04-10 08:07:12 +02:00
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-06 08:24:01 +02:00
|
|
|
executeListCommand: function(commandArray) {
|
2019-04-05 11:08:41 +02:00
|
|
|
const numArgs = commandArray.length;
|
|
|
|
function incorrectUsage() {
|
|
|
|
postError("Incorrect usage of ls command. Usage: ls [dir] [| grep pattern]");
|
2017-08-15 22:22:46 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
if (numArgs <= 0 || numArgs > 5 || numArgs === 3) {
|
|
|
|
return incorrectUsage();
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Grep
|
2019-04-05 11:08:41 +02:00
|
|
|
let filter = null; // Grep
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
// Directory path
|
|
|
|
let prefix = Terminal.currDir;
|
|
|
|
if (!prefix.endsWith("/")) {
|
|
|
|
prefix += "/";
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
|
|
|
|
// If there are 4+ arguments, then the last 3 must be for grep
|
|
|
|
if (numArgs >= 4) {
|
|
|
|
if (commandArray[numArgs - 2] !== "grep" || commandArray[numArgs - 3] !== "|") {
|
|
|
|
return incorrectUsage();
|
2017-08-15 22:22:46 +02:00
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
filter = commandArray[numArgs - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the second argument is not a pipe, then it must be for listing a directory
|
|
|
|
if (numArgs >= 2 && commandArray[1] !== "|") {
|
2019-04-10 08:07:12 +02:00
|
|
|
prefix = evaluateDirectoryPath(commandArray[1], Terminal.currDir);
|
|
|
|
if (prefix != null) {
|
|
|
|
if (!prefix.endsWith("/")) {
|
|
|
|
prefix += "/";
|
|
|
|
}
|
|
|
|
if (!isValidDirectoryPath(prefix)) {
|
|
|
|
return incorrectUsage();
|
|
|
|
}
|
2019-04-05 11:08:41 +02:00
|
|
|
}
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Root directory, which is the same as no 'prefix' at all
|
|
|
|
if (prefix === "/") {
|
|
|
|
prefix = null;
|
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Display all programs and scripts
|
2021-05-06 02:54:15 +02:00
|
|
|
const allPrograms = [];
|
|
|
|
const allScripts = [];
|
|
|
|
const allTextFiles = [];
|
|
|
|
const allContracts = [];
|
|
|
|
const allMessages = [];
|
|
|
|
const folders = [];
|
|
|
|
|
|
|
|
function handleFn(fn, dest) {
|
2019-04-10 08:07:12 +02:00
|
|
|
let parsedFn = fn;
|
2019-03-29 16:50:05 +01:00
|
|
|
if (prefix) {
|
|
|
|
if (!fn.startsWith(prefix)) {
|
|
|
|
return;
|
2017-08-18 19:20:51 +02:00
|
|
|
} else {
|
2019-04-10 08:07:12 +02:00
|
|
|
parsedFn = fn.slice(prefix.length, fn.length);
|
2017-08-18 19:20:51 +02:00
|
|
|
}
|
2017-08-15 22:22:46 +02:00
|
|
|
}
|
2019-03-29 16:50:05 +01:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
if (filter && !parsedFn.includes(filter)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
// If the fn includes a forward slash, it must be in a subdirectory.
|
|
|
|
// Therefore, we only list the "first" directory in its path
|
2019-04-10 08:07:12 +02:00
|
|
|
if (parsedFn.includes("/")) {
|
|
|
|
const firstParentDir = getFirstParentDirectory(parsedFn);
|
|
|
|
if (filter && !firstParentDir.includes(filter)) {
|
2019-03-29 16:50:05 +01:00
|
|
|
return;
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
if (!folders.includes(firstParentDir)) {
|
|
|
|
folders.push(firstParentDir);
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
return;
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
2019-03-29 16:50:05 +01:00
|
|
|
|
2021-05-06 02:54:15 +02:00
|
|
|
dest.push(parsedFn);
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Get all of the programs and scripts on the machine into one temporary array
|
2019-03-29 16:50:05 +01:00
|
|
|
const s = Player.getCurrentServer();
|
2021-05-06 02:54:15 +02:00
|
|
|
for (const program of s.programs) handleFn(program, allPrograms);
|
|
|
|
for (const script of s.scripts) handleFn(script.filename, allScripts);
|
|
|
|
for (const txt of s.textFiles) handleFn(txt.fn, allTextFiles);
|
|
|
|
for (const contract of s.contracts) handleFn(contract.fn, allContracts);
|
|
|
|
for (const msgOrLit of s.messages) (msgOrLit instanceof Message) ? handleFn(msgOrLit.filename, allMessages) : handleFn(msgOrLit, allMessages);
|
2019-03-29 16:50:05 +01:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Sort the files/folders alphabetically then print each
|
2021-05-06 02:54:15 +02:00
|
|
|
allPrograms.sort();
|
|
|
|
allScripts.sort();
|
|
|
|
allTextFiles.sort();
|
|
|
|
allContracts.sort();
|
|
|
|
allMessages.sort();
|
2019-04-10 08:07:12 +02:00
|
|
|
folders.sort();
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-05-06 02:54:15 +02:00
|
|
|
function postSegments(segments, config) {
|
|
|
|
const maxLength = Math.max(...segments.map(s => s.length))+1;
|
|
|
|
const filesPerRow = Math.floor(80 / maxLength);
|
|
|
|
for(let i = 0; i < segments.length; i++) {
|
|
|
|
let row = '';
|
|
|
|
for(let col = 0; col < filesPerRow; col++) {
|
|
|
|
if(!(i < segments.length)) break;
|
|
|
|
row += segments[i];
|
|
|
|
row += " ".repeat((maxLength * (col+1)) - row.length);
|
|
|
|
i++
|
|
|
|
}
|
2021-05-09 04:38:48 +02:00
|
|
|
i--;
|
2021-05-06 02:54:15 +02:00
|
|
|
postContent(row, config);
|
|
|
|
}
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-05-06 02:54:15 +02:00
|
|
|
|
|
|
|
const config = { color: "#0000FF" };
|
2021-05-18 03:58:11 +02:00
|
|
|
const groups = [
|
|
|
|
{segments: folders, config: config},
|
|
|
|
{segments: allMessages},
|
|
|
|
{segments: allTextFiles},
|
|
|
|
{segments: allPrograms},
|
|
|
|
{segments: allContracts},
|
|
|
|
{segments: allScripts},
|
|
|
|
].filter((g) => g.segments.length > 0)
|
|
|
|
for(let i = 0; i < groups.length; i++) {
|
|
|
|
if(i !== 0) postElement(<br />);
|
|
|
|
postSegments(groups[i].segments, groups[i].config);
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
},
|
2019-02-05 07:45:04 +01:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
executeMemCommand: function(commandArray) {
|
|
|
|
try {
|
|
|
|
if (commandArray.length !== 2 && commandArray.length !== 4) {
|
|
|
|
postError("Incorrect usage of mem command. usage: mem [scriptname] [-t] [number threads]");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
const scriptName = commandArray[1];
|
|
|
|
let numThreads = 1;
|
|
|
|
if (commandArray.length === 4 && commandArray[2] === "-t") {
|
|
|
|
numThreads = Math.round(parseInt(commandArray[3]));
|
|
|
|
if (isNaN(numThreads) || numThreads < 1) {
|
|
|
|
postError("Invalid number of threads specified. Number of threads must be greater than 1");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
const script = Terminal.getScript(scriptName);
|
|
|
|
if (script == null) {
|
|
|
|
postError("No such script exists!");
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
const ramUsage = script.ramUsage * numThreads;
|
|
|
|
|
2021-03-31 06:45:21 +02:00
|
|
|
post(`This script requires ${numeralWrapper.formatRAM(ramUsage)} of RAM to run for ${numThreads} thread(s)`);
|
2019-04-10 08:07:12 +02:00
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
executeNanoCommand: function(commandArray) {
|
|
|
|
if (commandArray.length !== 2) {
|
|
|
|
postError("Incorrect usage of nano command. Usage: nano [scriptname]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
try {
|
|
|
|
const filename = commandArray[1];
|
|
|
|
if (filename === ".fconf") {
|
|
|
|
let text = createFconf();
|
|
|
|
Engine.loadScriptEditorContent(filename, text);
|
|
|
|
return;
|
|
|
|
} else if (isScriptFilename(filename)) {
|
|
|
|
const filepath = Terminal.getFilepath(filename);
|
|
|
|
const script = Terminal.getScript(filename);
|
|
|
|
if (script == null) {
|
2021-03-07 19:11:29 +01:00
|
|
|
let code = ""
|
2021-04-18 17:18:56 +02:00
|
|
|
if(filename.endsWith(".ns") || filename.endsWith(".js")) {
|
2021-03-07 19:11:29 +01:00
|
|
|
code = `export async function main(ns) {
|
2021-04-29 02:07:26 +02:00
|
|
|
|
2021-03-07 19:11:29 +01:00
|
|
|
}`;
|
|
|
|
}
|
|
|
|
Engine.loadScriptEditorContent(filepath, code);
|
2021-04-20 03:26:51 +02:00
|
|
|
getCurrentEditor().setCursor({row: 1, column: 4});
|
2019-04-10 08:07:12 +02:00
|
|
|
} else {
|
|
|
|
Engine.loadScriptEditorContent(filepath, script.code);
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
} else if (filename.endsWith(".txt")) {
|
|
|
|
const filepath = Terminal.getFilepath(filename);
|
|
|
|
const txt = Terminal.getTextFile(filename);
|
|
|
|
if (txt == null) {
|
|
|
|
Engine.loadScriptEditorContent(filepath);
|
|
|
|
} else {
|
|
|
|
Engine.loadScriptEditorContent(filepath, txt.text);
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
} else {
|
|
|
|
postError("Invalid file. Only scripts (.script, .ns, .js), text files (.txt), or .fconf can be edited with nano"); return;
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-06 08:24:01 +02:00
|
|
|
executeScanCommand: function(commandArray) {
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of netstat/scan command. Usage: netstat/scan");
|
|
|
|
return;
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
|
|
|
|
// Displays available network connections using TCP
|
|
|
|
const currServ = Player.getCurrentServer();
|
2017-05-06 08:24:01 +02:00
|
|
|
post("Hostname IP Root Access");
|
2019-03-05 02:40:28 +01:00
|
|
|
for (let i = 0; i < currServ.serversOnNetwork.length; i++) {
|
2019-04-13 03:22:46 +02:00
|
|
|
// Add hostname
|
2019-03-05 02:40:28 +01:00
|
|
|
let entry = getServerOnNetwork(currServ, i);
|
2019-02-05 07:45:04 +01:00
|
|
|
if (entry == null) { continue; }
|
2017-05-08 18:00:34 +02:00
|
|
|
entry = entry.hostname;
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Calculate padding and add IP
|
2019-02-05 07:45:04 +01:00
|
|
|
let numSpaces = 21 - entry.length;
|
|
|
|
let spaces = Array(numSpaces+1).join(" ");
|
2017-05-06 08:24:01 +02:00
|
|
|
entry += spaces;
|
2019-03-05 02:40:28 +01:00
|
|
|
entry += getServerOnNetwork(currServ, i).ip;
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Calculate padding and add root access info
|
2019-02-05 07:45:04 +01:00
|
|
|
let hasRoot;
|
2019-03-05 02:40:28 +01:00
|
|
|
if (getServerOnNetwork(currServ, i).hasAdminRights) {
|
2017-05-06 08:24:01 +02:00
|
|
|
hasRoot = 'Y';
|
|
|
|
} else {
|
|
|
|
hasRoot = 'N';
|
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
numSpaces = 21 - getServerOnNetwork(currServ, i).ip.length;
|
2017-05-06 08:24:01 +02:00
|
|
|
spaces = Array(numSpaces+1).join(" ");
|
|
|
|
entry += spaces;
|
|
|
|
entry += hasRoot;
|
|
|
|
post(entry);
|
|
|
|
}
|
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-09-19 20:38:03 +02:00
|
|
|
executeScanAnalyzeCommand: function(depth=1, all=false) {
|
2019-04-13 03:22:46 +02:00
|
|
|
// TODO Using array as stack for now, can make more efficient
|
2017-05-24 23:35:24 +02:00
|
|
|
post("~~~~~~~~~~ Beginning scan-analyze ~~~~~~~~~~");
|
|
|
|
post(" ");
|
2019-02-06 08:06:48 +01:00
|
|
|
|
|
|
|
// Map of all servers to keep track of which have been visited
|
|
|
|
var visited = {};
|
|
|
|
for (const ip in AllServers) {
|
|
|
|
visited[ip] = 0;
|
|
|
|
}
|
2017-09-19 20:38:03 +02:00
|
|
|
|
2019-03-27 09:36:14 +01:00
|
|
|
const stack = [];
|
|
|
|
const depthQueue = [0];
|
|
|
|
const currServ = Player.getCurrentServer();
|
2017-05-24 19:07:33 +02:00
|
|
|
stack.push(currServ);
|
|
|
|
while(stack.length != 0) {
|
2019-03-27 09:36:14 +01:00
|
|
|
const s = stack.pop();
|
|
|
|
const d = depthQueue.pop();
|
|
|
|
const isHacknet = s instanceof HacknetServer;
|
2017-09-19 20:38:03 +02:00
|
|
|
if (!all && s.purchasedByPlayer && s.hostname != "home") {
|
2019-03-27 09:36:14 +01:00
|
|
|
continue; // Purchased server
|
2017-09-19 20:38:03 +02:00
|
|
|
} else if (visited[s.ip] || d > depth) {
|
2019-03-27 09:36:14 +01:00
|
|
|
continue; // Already visited or out-of-depth
|
|
|
|
} else if (!all && isHacknet) {
|
|
|
|
continue; // Hacknet Server
|
2017-05-24 19:07:33 +02:00
|
|
|
} else {
|
|
|
|
visited[s.ip] = 1;
|
|
|
|
}
|
|
|
|
for (var i = s.serversOnNetwork.length-1; i >= 0; --i) {
|
2019-03-05 02:40:28 +01:00
|
|
|
stack.push(getServerOnNetwork(s, i));
|
2017-05-24 19:07:33 +02:00
|
|
|
depthQueue.push(d+1);
|
|
|
|
}
|
2019-04-13 03:22:46 +02:00
|
|
|
if (d == 0) {continue;} // Don't print current server
|
2017-05-24 23:35:24 +02:00
|
|
|
var titleDashes = Array((d-1) * 4 + 1).join("-");
|
2018-06-02 01:31:34 +02:00
|
|
|
if (Player.hasProgram(Programs.AutoLink.name)) {
|
2017-06-23 16:23:35 +02:00
|
|
|
post("<strong>" + titleDashes + "> <a class='scan-analyze-link'>" + s.hostname + "</a></strong>", false);
|
|
|
|
} else {
|
|
|
|
post("<strong>" + titleDashes + ">" + s.hostname + "</strong>");
|
|
|
|
}
|
|
|
|
|
2017-05-24 23:35:24 +02:00
|
|
|
var dashes = titleDashes + "--";
|
|
|
|
var c = "NO";
|
|
|
|
if (s.hasAdminRights) {c = "YES";}
|
2019-03-30 00:14:32 +01:00
|
|
|
post(`${dashes}Root Access: ${c}${!isHacknet ? ", Required hacking skill: " + s.requiredHackingSkill : ""}`);
|
2019-03-27 09:36:14 +01:00
|
|
|
if (!isHacknet) { post(dashes + "Number of open ports required to NUKE: " + s.numOpenPortsRequired); }
|
2021-03-31 06:45:21 +02:00
|
|
|
post(dashes + "RAM: " + numeralWrapper.formatRAM(s.maxRam));
|
2017-05-23 19:36:35 +02:00
|
|
|
post(" ");
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-06-23 16:23:35 +02:00
|
|
|
var links = document.getElementsByClassName("scan-analyze-link");
|
2019-02-05 07:45:04 +01:00
|
|
|
for (let i = 0; i < links.length; ++i) {
|
2017-06-23 16:23:35 +02:00
|
|
|
(function() {
|
|
|
|
var hostname = links[i].innerHTML.toString();
|
|
|
|
links[i].onclick = function() {
|
2017-09-15 16:06:59 +02:00
|
|
|
if (Terminal.analyzeFlag || Terminal.hackFlag) {return;}
|
2017-06-23 16:23:35 +02:00
|
|
|
Terminal.connectToServer(hostname);
|
|
|
|
}
|
2019-04-13 03:22:46 +02:00
|
|
|
}());// Immediate invocation
|
2017-06-23 16:23:35 +02:00
|
|
|
}
|
2017-05-23 19:36:35 +02:00
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
executeScpCommand(commandArray) {
|
2019-04-10 08:07:12 +02:00
|
|
|
try {
|
|
|
|
if (commandArray.length !== 3) {
|
|
|
|
postError("Incorrect usage of scp command. Usage: scp [file] [destination hostname/ip]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const scriptname = Terminal.getFilepath(commandArray[1]);
|
|
|
|
if (!scriptname.endsWith(".lit") && !isScriptFilename(scriptname) && !scriptname.endsWith(".txt")) {
|
|
|
|
postError("scp only works for scripts, text files (.txt), and literature files (.lit)");
|
|
|
|
return;
|
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
const destServer = getServer(commandArray[2]);
|
|
|
|
if (destServer == null) {
|
|
|
|
postError(`Invalid destination. ${commandArray[2]} not found`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const currServ = Player.getCurrentServer();
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Scp for lit files
|
2019-04-10 08:07:12 +02:00
|
|
|
if (scriptname.endsWith(".lit")) {
|
|
|
|
var found = false;
|
|
|
|
for (var i = 0; i < currServ.messages.length; ++i) {
|
|
|
|
if (!(currServ.messages[i] instanceof Message) && currServ.messages[i] == scriptname) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) { return postError("No such file exists!"); }
|
|
|
|
|
|
|
|
for (var i = 0; i < destServer.messages.length; ++i) {
|
|
|
|
if (destServer.messages[i] === scriptname) {
|
|
|
|
post(scriptname + " copied over to " + destServer.hostname);
|
2019-04-13 03:22:46 +02:00
|
|
|
return; // Already exists
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
destServer.messages.push(scriptname);
|
|
|
|
return post(scriptname + " copied over to " + destServer.hostname);
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Scp for txt files
|
2019-04-10 08:07:12 +02:00
|
|
|
if (scriptname.endsWith(".txt")) {
|
|
|
|
var found = false, txtFile;
|
|
|
|
for (var i = 0; i < currServ.textFiles.length; ++i) {
|
|
|
|
if (currServ.textFiles[i].fn === scriptname) {
|
|
|
|
found = true;
|
|
|
|
txtFile = currServ.textFiles[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) { return postError("No such file exists!"); }
|
2019-02-05 07:45:04 +01:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
let tRes = destServer.writeToTextFile(txtFile.fn, txtFile.text);
|
|
|
|
if (!tRes.success) {
|
|
|
|
postError("scp failed");
|
|
|
|
return;
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
if (tRes.overwritten) {
|
|
|
|
post(`WARNING: ${scriptname} already exists on ${destServer.hostname} and will be overwriten`);
|
|
|
|
post(`${scriptname} overwritten on ${destServer.hostname}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
post(`${scriptname} copied over to ${destServer.hostname}`);
|
|
|
|
return;
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Get the current script
|
|
|
|
let sourceScript = null;
|
|
|
|
for (let i = 0; i < currServ.scripts.length; ++i) {
|
|
|
|
if (scriptname == currServ.scripts[i].filename) {
|
|
|
|
sourceScript = currServ.scripts[i];
|
2019-02-05 07:45:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
if (sourceScript == null) {
|
|
|
|
postError("scp() failed. No such script exists");
|
|
|
|
return;
|
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
let sRes = destServer.writeToScriptFile(scriptname, sourceScript.code);
|
|
|
|
if (!sRes.success) {
|
|
|
|
postError(`scp failed`);
|
2019-02-05 07:45:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
if (sRes.overwritten) {
|
|
|
|
post(`WARNING: ${scriptname} already exists on ${destServer.hostname} and will be overwritten`);
|
2019-02-05 07:45:04 +01:00
|
|
|
post(`${scriptname} overwritten on ${destServer.hostname}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
post(`${scriptname} copied over to ${destServer.hostname}`);
|
2019-04-10 08:07:12 +02:00
|
|
|
} catch(e) {
|
|
|
|
Terminal.postThrownError(e);
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
2017-05-06 08:24:01 +02:00
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// First called when the "run [program]" command is called. Checks to see if you
|
|
|
|
// have the executable and, if you do, calls the executeProgram() function
|
|
|
|
runProgram: function(commandArray) {
|
2019-02-20 09:42:27 +01:00
|
|
|
if (commandArray.length < 2) { return; }
|
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// Check if you have the program on your computer. If you do, execute it, otherwise
|
|
|
|
// display an error message
|
2019-02-20 09:42:27 +01:00
|
|
|
const programName = commandArray[1];
|
|
|
|
|
|
|
|
if (Player.hasProgram(programName)) {
|
|
|
|
Terminal.executeProgram(commandArray);
|
2017-06-23 16:23:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
post("ERROR: No such executable on home computer (Only programs that exist on your home computer can be run)");
|
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// Contains the implementations of all possible programs
|
|
|
|
executeProgram: function(commandArray) {
|
2019-02-20 09:42:27 +01:00
|
|
|
if (commandArray.length < 2) { return; }
|
|
|
|
|
2017-04-19 23:40:26 +02:00
|
|
|
var s = Player.getCurrentServer();
|
2019-02-20 09:42:27 +01:00
|
|
|
const programName = commandArray[1];
|
|
|
|
const splitArgs = commandArray.slice(1);
|
2018-01-27 07:52:39 +01:00
|
|
|
|
2018-07-31 06:18:38 +02:00
|
|
|
// TODO: refactor this/these out of Terminal. This logic could reside closer to the Programs themselves.
|
|
|
|
/**
|
|
|
|
* @typedef {function (server=, args=)} ProgramHandler
|
|
|
|
* @param {Server} server The current server the program is being executed against
|
|
|
|
* @param {string[]} args The command line arguments passed in to the program
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
/**
|
2018-08-29 21:06:21 +02:00
|
|
|
* @type {Object.<string, ProgramHandler}
|
2018-07-31 06:18:38 +02:00
|
|
|
*/
|
|
|
|
const programHandlers = {};
|
|
|
|
programHandlers[Programs.NukeProgram.name] = (server) => {
|
|
|
|
if (server.hasAdminRights) {
|
|
|
|
post("You already have root access to this computer. There is no reason to run NUKE.exe");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server.openPortCount >= Player.getCurrentServer().numOpenPortsRequired) {
|
|
|
|
server.hasAdminRights = true;
|
|
|
|
post("NUKE successful! Gained root access to " + Player.getCurrentServer().hostname);
|
|
|
|
// TODO: Make this take time rather than be instant
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
post("NUKE unsuccessful. Not enough ports have been opened");
|
|
|
|
};
|
|
|
|
programHandlers[Programs.BruteSSHProgram.name] = (server) => {
|
|
|
|
if (server.sshPortOpen) {
|
|
|
|
post("SSH Port (22) is already open!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.sshPortOpen = true;
|
|
|
|
post("Opened SSH Port(22)!")
|
|
|
|
server.openPortCount++;
|
|
|
|
};
|
|
|
|
programHandlers[Programs.FTPCrackProgram.name] = (server) => {
|
|
|
|
if (server.ftpPortOpen) {
|
|
|
|
post("FTP Port (21) is already open!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.ftpPortOpen = true;
|
|
|
|
post("Opened FTP Port (21)!");
|
|
|
|
server.openPortCount++;
|
|
|
|
};
|
|
|
|
programHandlers[Programs.RelaySMTPProgram.name] = (server) => {
|
|
|
|
if (server.smtpPortOpen) {
|
|
|
|
post("SMTP Port (25) is already open!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.smtpPortOpen = true;
|
|
|
|
post("Opened SMTP Port (25)!");
|
|
|
|
server.openPortCount++;
|
|
|
|
};
|
|
|
|
programHandlers[Programs.HTTPWormProgram.name] = (server) => {
|
2018-08-07 01:11:14 +02:00
|
|
|
if (server.httpPortOpen) {
|
2018-07-31 06:18:38 +02:00
|
|
|
post("HTTP Port (80) is already open!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.httpPortOpen = true;
|
|
|
|
post("Opened HTTP Port (80)!");
|
|
|
|
server.openPortCount++;
|
|
|
|
};
|
|
|
|
programHandlers[Programs.SQLInjectProgram.name] = (server) => {
|
|
|
|
if (server.sqlPortOpen) {
|
|
|
|
post("SQL Port (1433) is already open!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.sqlPortOpen = true;
|
|
|
|
post("Opened SQL Port (1433)!");
|
|
|
|
server.openPortCount++;
|
|
|
|
};
|
|
|
|
programHandlers[Programs.ServerProfiler.name] = (server, args) => {
|
|
|
|
if (args.length !== 2) {
|
|
|
|
post("Must pass a server hostname or IP as an argument for ServerProfiler.exe");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetServer = getServer(args[1]);
|
|
|
|
if (targetServer == null) {
|
|
|
|
post("Invalid server IP/hostname");
|
|
|
|
return;
|
|
|
|
}
|
2021-05-03 08:20:31 +02:00
|
|
|
|
|
|
|
if(targetServer instanceof HacknetServer) {
|
|
|
|
post(`${Programs.ServerProfiler.name} cannot be run on a Hacknet Server.`);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-31 06:18:38 +02:00
|
|
|
post(targetServer.hostname + ":");
|
|
|
|
post("Server base security level: " + targetServer.baseDifficulty);
|
|
|
|
post("Server current security level: " + targetServer.hackDifficulty);
|
|
|
|
post("Server growth rate: " + targetServer.serverGrowth);
|
2021-05-02 07:04:49 +02:00
|
|
|
post(`Netscript hack() execution time: ${convertTimeMsToTimeElapsedString(calculateHackingTime(targetServer, Player)*1000, true)}`);
|
|
|
|
post(`Netscript grow() execution time: ${convertTimeMsToTimeElapsedString(calculateGrowTime(targetServer, Player)*1000, true)}`);
|
|
|
|
post(`Netscript weaken() execution time: ${convertTimeMsToTimeElapsedString(calculateWeakenTime(targetServer, Player)*1000, true)}`);
|
2018-07-31 06:18:38 +02:00
|
|
|
};
|
|
|
|
programHandlers[Programs.AutoLink.name] = () => {
|
|
|
|
post("This executable cannot be run.");
|
|
|
|
post("AutoLink.exe lets you automatically connect to other servers when using 'scan-analyze'.");
|
|
|
|
post("When using scan-analyze, click on a server's hostname to connect to it.");
|
|
|
|
};
|
|
|
|
programHandlers[Programs.DeepscanV1.name] = () => {
|
|
|
|
post("This executable cannot be run.");
|
|
|
|
post("DeepscanV1.exe lets you run 'scan-analyze' with a depth up to 5.");
|
|
|
|
};
|
|
|
|
programHandlers[Programs.DeepscanV2.name] = () => {
|
|
|
|
post("This executable cannot be run.");
|
|
|
|
post("DeepscanV2.exe lets you run 'scan-analyze' with a depth up to 10.");
|
|
|
|
};
|
|
|
|
programHandlers[Programs.Flight.name] = () => {
|
2019-03-29 17:10:22 +01:00
|
|
|
const numAugReq = Math.round(BitNodeMultipliers.DaedalusAugsRequirement*30)
|
|
|
|
const fulfilled = Player.augmentations.length >= numAugReq &&
|
2018-07-31 06:18:38 +02:00
|
|
|
Player.money.gt(1e11) &&
|
2021-04-06 09:50:09 +02:00
|
|
|
Player.hacking_skill >= 2500;
|
2018-07-31 06:18:38 +02:00
|
|
|
if(!fulfilled) {
|
2019-03-29 17:10:22 +01:00
|
|
|
post(`Augmentations: ${Player.augmentations.length} / ${numAugReq}`);
|
2021-03-31 06:45:21 +02:00
|
|
|
postElement(<>Money: {Money(Player.money.toNumber())} / {Money(1e11)}</>);
|
2019-03-29 17:10:22 +01:00
|
|
|
post(`Hacking skill: ${Player.hacking_skill} / 2500`);
|
2018-07-31 06:18:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
post("We will contact you.");
|
|
|
|
post("-- Daedalus --");
|
|
|
|
};
|
|
|
|
programHandlers[Programs.BitFlume.name] = () => {
|
|
|
|
const yesBtn = yesNoBoxGetYesButton();
|
|
|
|
const noBtn = yesNoBoxGetNoButton();
|
|
|
|
yesBtn.innerHTML = "Travel to BitNode Nexus";
|
|
|
|
noBtn.innerHTML = "Cancel";
|
|
|
|
yesBtn.addEventListener("click", function() {
|
|
|
|
hackWorldDaemon(Player.bitNodeN, true);
|
|
|
|
return yesNoBoxClose();
|
|
|
|
});
|
|
|
|
noBtn.addEventListener("click", function() {
|
|
|
|
return yesNoBoxClose();
|
|
|
|
});
|
|
|
|
yesNoBoxCreate("WARNING: USING THIS PROGRAM WILL CAUSE YOU TO LOSE ALL OF YOUR PROGRESS ON THE CURRENT BITNODE.<br><br>" +
|
|
|
|
"Do you want to travel to the BitNode Nexus? This allows you to reset the current BitNode " +
|
|
|
|
"and select a new one.");
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!programHandlers.hasOwnProperty(programName)){
|
|
|
|
post("Invalid executable. Cannot be run");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
programHandlers[programName](s, splitArgs);
|
2021-04-18 17:18:56 +02:00
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
/**
|
|
|
|
* Given a filename, returns that file's full path. This takes into account
|
|
|
|
* the Terminal's current directory.
|
|
|
|
*/
|
|
|
|
getFilepath : function(filename) {
|
|
|
|
const path = evaluateFilePath(filename, Terminal.currDir);
|
|
|
|
if (path == null) {
|
|
|
|
throw new Error(`Invalid file path specified: ${filename}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInRootDirectory(path)) {
|
|
|
|
return removeLeadingSlash(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a filename, searches and returns that file. File-type agnostic
|
|
|
|
*/
|
|
|
|
getFile: function (filename) {
|
|
|
|
if (isScriptFilename(filename)) {
|
|
|
|
return Terminal.getScript(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filename.endsWith(".lit")) {
|
|
|
|
return Terminal.getLitFile(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filename.endsWith(".txt")) {
|
|
|
|
return Terminal.getTextFile(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a file path referring to a literature file, taking into account the terminal's
|
|
|
|
* current directory + server. Returns the lit file if it exists, and null otherwise
|
|
|
|
*/
|
|
|
|
getLitFile: function(filename) {
|
|
|
|
const s = Player.getCurrentServer();
|
|
|
|
const filepath = Terminal.getFilepath(filename);
|
|
|
|
for (const lit of s.messages) {
|
|
|
|
if (typeof lit === "string" && filepath === lit) {
|
|
|
|
return lit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a file path referring to a script, taking into account the terminal's
|
|
|
|
* current directory + server. Returns the script if it exists, and null otherwise.
|
|
|
|
*/
|
|
|
|
getScript: function(filename) {
|
|
|
|
const s = Player.getCurrentServer();
|
|
|
|
const filepath = Terminal.getFilepath(filename);
|
|
|
|
for (const script of s.scripts) {
|
|
|
|
if (filepath === script.filename) {
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a file path referring to a text file, taking into account the terminal's
|
|
|
|
* current directory + server. Returns the text file if it exists, and null otherwise.
|
|
|
|
*/
|
|
|
|
getTextFile: function(filename) {
|
|
|
|
const s = Player.getCurrentServer();
|
|
|
|
const filepath = Terminal.getFilepath(filename);
|
|
|
|
for (const txt of s.textFiles) {
|
|
|
|
if (filepath === txt.fn) {
|
|
|
|
return txt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
postThrownError: function(e) {
|
|
|
|
if (e instanceof Error) {
|
|
|
|
const errorLabel = "Error: ";
|
|
|
|
const errorString = e.toString();
|
|
|
|
if (errorString.startsWith(errorLabel)) {
|
|
|
|
postError(errorString.slice(errorLabel.length));
|
|
|
|
} else {
|
|
|
|
postError(errorString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
runScript: function(commandArray) {
|
2019-02-06 03:32:15 +01:00
|
|
|
if (commandArray.length < 2) {
|
|
|
|
dialogBoxCreate(`Bug encountered with Terminal.runScript(). Command array has a length of less than 2: ${commandArray}`);
|
|
|
|
return;
|
2017-06-11 03:46:02 +02:00
|
|
|
}
|
2019-02-06 03:32:15 +01:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
const server = Player.getCurrentServer();
|
2019-02-06 03:32:15 +01:00
|
|
|
|
|
|
|
let numThreads = 1;
|
|
|
|
const args = [];
|
2019-04-10 08:07:12 +02:00
|
|
|
const scriptName = Terminal.getFilepath(commandArray[1]);
|
2019-02-06 03:32:15 +01:00
|
|
|
|
|
|
|
if (commandArray.length > 2) {
|
|
|
|
if (commandArray.length >= 4 && commandArray[2] == "-t") {
|
|
|
|
numThreads = Math.round(parseFloat(commandArray[3]));
|
2017-06-17 04:53:57 +02:00
|
|
|
if (isNaN(numThreads) || numThreads < 1) {
|
2019-02-06 03:32:15 +01:00
|
|
|
postError("Invalid number of threads specified. Number of threads must be greater than 0");
|
2017-06-17 04:53:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-02-06 03:32:15 +01:00
|
|
|
for (let i = 4; i < commandArray.length; ++i) {
|
|
|
|
args.push(commandArray[i]);
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-06 03:32:15 +01:00
|
|
|
for (let i = 2; i < commandArray.length; ++i) {
|
|
|
|
args.push(commandArray[i])
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Check if this script is already running
|
2017-06-17 04:53:57 +02:00
|
|
|
if (findRunningScript(scriptName, args, server) != null) {
|
|
|
|
post("ERROR: This script is already running. Cannot run multiple instances");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
// Check if the script exists and if it does run it
|
|
|
|
for (var i = 0; i < server.scripts.length; i++) {
|
|
|
|
if (server.scripts[i].filename === scriptName) {
|
|
|
|
// Check for admin rights and that there is enough RAM availble to run
|
2017-06-11 03:46:02 +02:00
|
|
|
var script = server.scripts[i];
|
2021-04-18 17:18:56 +02:00
|
|
|
var ramUsage = script.ramUsage * numThreads;
|
|
|
|
var ramAvailable = server.maxRam - server.ramUsed;
|
|
|
|
|
|
|
|
if (server.hasAdminRights == false) {
|
|
|
|
post("Need root access to run script");
|
|
|
|
return;
|
|
|
|
} else if (ramUsage > ramAvailable){
|
|
|
|
post("This machine does not have enough RAM to run this script with " +
|
2017-06-17 04:53:57 +02:00
|
|
|
numThreads + " threads. Script requires " + ramUsage + "GB of RAM");
|
2021-04-18 17:18:56 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Able to run script
|
2017-06-17 04:53:57 +02:00
|
|
|
var runningScriptObj = new RunningScript(script, args);
|
|
|
|
runningScriptObj.threads = numThreads;
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-07-14 05:55:58 +02:00
|
|
|
if (startWorkerScript(runningScriptObj, server)) {
|
2021-04-29 02:07:26 +02:00
|
|
|
post(`Running script with ${numThreads} thread(s), pid ${runningScriptObj.pid} and args: ${arrayToString(args)}.`);
|
2019-07-14 05:55:58 +02:00
|
|
|
} else {
|
|
|
|
postError(`Failed to start script`);
|
|
|
|
}
|
|
|
|
return;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-04-18 17:18:56 +02:00
|
|
|
post("ERROR: No such script");
|
|
|
|
},
|
2018-09-14 23:03:31 +02:00
|
|
|
|
|
|
|
runContract: async function(contractName) {
|
|
|
|
// There's already an opened contract
|
|
|
|
if (Terminal.contractOpen) {
|
|
|
|
return post("ERROR: There's already a Coding Contract in Progress");
|
|
|
|
}
|
|
|
|
|
|
|
|
const serv = Player.getCurrentServer();
|
|
|
|
const contract = serv.getContract(contractName);
|
|
|
|
if (contract == null) {
|
|
|
|
return post("ERROR: No such contract");
|
|
|
|
}
|
2018-09-23 02:25:48 +02:00
|
|
|
|
|
|
|
Terminal.contractOpen = true;
|
2018-09-14 23:03:31 +02:00
|
|
|
const res = await contract.prompt();
|
|
|
|
|
|
|
|
switch (res) {
|
|
|
|
case CodingContractResult.Success:
|
|
|
|
var reward = Player.gainCodingContractReward(contract.reward, contract.getDifficulty());
|
|
|
|
post(`Contract SUCCESS - ${reward}`);
|
|
|
|
serv.removeContract(contract);
|
|
|
|
break;
|
|
|
|
case CodingContractResult.Failure:
|
|
|
|
++contract.tries;
|
|
|
|
if (contract.tries >= contract.getMaxNumTries()) {
|
2018-09-23 02:25:48 +02:00
|
|
|
post("Contract <p style='color:red;display:inline'>FAILED</p> - Contract is now self-destructing");
|
2018-09-14 23:03:31 +02:00
|
|
|
serv.removeContract(contract);
|
2018-09-23 02:25:48 +02:00
|
|
|
} else {
|
|
|
|
post(`Contract <p style='color:red;display:inline'>FAILED</p> - ${contract.getMaxNumTries() - contract.tries} tries remaining`);
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CodingContractResult.Cancelled:
|
|
|
|
default:
|
|
|
|
post("Contract cancelled");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Terminal.contractOpen = false;
|
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
};
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-04-29 02:07:26 +02:00
|
|
|
export {postNetburnerText, Terminal};
|