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
|
|
|
isValidFilename,
|
|
|
|
removeLeadingSlash,
|
|
|
|
removeTrailingSlash
|
|
|
|
} 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";
|
2019-04-10 08:07:12 +02:00
|
|
|
|
|
|
|
import {
|
2019-04-11 10:37:40 +02:00
|
|
|
Aliases,
|
|
|
|
GlobalAliases,
|
|
|
|
parseAliasDeclaration,
|
|
|
|
printAliases,
|
|
|
|
removeAlias,
|
|
|
|
substituteAliases
|
|
|
|
} from "./Alias";
|
|
|
|
import { BitNodeMultipliers } from "./BitNode/BitNodeMultipliers";
|
2019-04-10 08:07:12 +02:00
|
|
|
import {
|
2019-04-11 10:37:40 +02:00
|
|
|
CodingContract,
|
|
|
|
CodingContractResult,
|
|
|
|
CodingContractRewardType
|
|
|
|
} 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,
|
|
|
|
checkIfConnectedToDarkweb
|
|
|
|
} from "./DarkWeb/DarkWeb";
|
|
|
|
import { DarkWebItems } from "./DarkWeb/DarkWebItems";
|
|
|
|
import { Engine } from "./engine";
|
|
|
|
import { parseFconfSettings, createFconf } from "./Fconf/Fconf";
|
|
|
|
import { FconfSettings } from "./Fconf/FconfSettings";
|
|
|
|
import {
|
|
|
|
calculateHackingChance,
|
|
|
|
calculateHackingExpGain,
|
|
|
|
calculatePercentMoneyHacked,
|
|
|
|
calculateHackingTime,
|
|
|
|
calculateGrowTime,
|
|
|
|
calculateWeakenTime
|
|
|
|
} from "./Hacking";
|
|
|
|
import { HacknetServer } from "./Hacknet/HacknetServer";
|
|
|
|
import {
|
|
|
|
iTutorialNextStep,
|
|
|
|
iTutorialSteps,
|
|
|
|
ITutorial
|
|
|
|
} from "./InteractiveTutorial";
|
|
|
|
import { showLiterature } from "./Literature";
|
|
|
|
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";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { findRunningScript } from "./Script/ScriptHelpers";
|
|
|
|
import { isScriptFilename } from "./Script/ScriptHelpersTS";
|
|
|
|
import { AllServers } from "./Server/AllServers";
|
|
|
|
import { Server } from "./Server/Server";
|
|
|
|
import {
|
|
|
|
GetServerByHostname,
|
|
|
|
getServer,
|
|
|
|
getServerOnNetwork
|
|
|
|
} from "./Server/ServerHelpers";
|
|
|
|
import { Settings } from "./Settings/Settings";
|
|
|
|
import {
|
|
|
|
SpecialServerIps,
|
|
|
|
SpecialServerNames
|
|
|
|
} from "./Server/SpecialServerIps";
|
|
|
|
import { getTextFile } from "./TextFile";
|
|
|
|
import { setTimeoutRef } from "./utils/SetTimeoutRef";
|
|
|
|
import { Page, routing } from "./ui/navigationTracking";
|
|
|
|
import { numeralWrapper } from "./ui/numeralFormat";
|
|
|
|
import { KEY } from "../utils/helpers/keyCodes";
|
|
|
|
import { addOffset } from "../utils/helpers/addOffset";
|
|
|
|
import { isString } from "../utils/helpers/isString";
|
|
|
|
import { arrayToString } from "../utils/helpers/arrayToString";
|
|
|
|
import { getTimestamp } from "../utils/helpers/getTimestamp";
|
|
|
|
import { logBoxCreate } from "../utils/LogBox";
|
|
|
|
import {
|
|
|
|
yesNoBoxCreate,
|
|
|
|
yesNoBoxGetYesButton,
|
|
|
|
yesNoBoxGetNoButton,
|
|
|
|
yesNoBoxClose
|
|
|
|
} from "../utils/YesNoBox";
|
2019-04-10 08:07:12 +02:00
|
|
|
import {
|
|
|
|
post,
|
|
|
|
postContent,
|
|
|
|
postError,
|
|
|
|
hackProgressBarPost,
|
|
|
|
hackProgressPost
|
|
|
|
} from "./ui/postToTerminal";
|
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";
|
|
|
|
|
2018-03-12 20:39:04 +01:00
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function postNetburnerText() {
|
2017-04-24 21:10:35 +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));
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-04-13 03:22:46 +02:00
|
|
|
// Terminal
|
2018-07-20 03:51:18 +02:00
|
|
|
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
|
|
|
|
2018-03-12 20:39:04 +01:00
|
|
|
if (event.keyCode === KEY.ENTER) {
|
2019-04-13 03:22:46 +02:00
|
|
|
event.preventDefault(); // Prevent newline from being entered in Script Editor
|
2019-04-05 11:08:41 +02:00
|
|
|
const command = terminalInput.value;
|
2019-04-10 08:07:12 +02:00
|
|
|
const dir = Terminal.currDir;
|
2018-08-29 05:24:38 +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 +
|
2019-04-05 11:08:41 +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);
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
2016-10-17 10:24:39 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-03-12 20:39:04 +01:00
|
|
|
if (event.keyCode === KEY.C && event.ctrlKey) {
|
|
|
|
if (Engine._actionInProgress) {
|
2019-04-13 03:22:46 +02:00
|
|
|
// Cancel action
|
2018-03-12 20:39:04 +01:00
|
|
|
post("Cancelling...");
|
|
|
|
Engine._actionInProgress = false;
|
|
|
|
Terminal.finishAction(true);
|
|
|
|
} 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
|
|
|
}
|
2016-12-21 17:33:00 +01: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;
|
2019-02-20 09:42:27 +01:00
|
|
|
setTimeoutRef(function(){terminalInput.selectionStart = terminalInput.selectionEnd = 10000; }, 0);
|
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
|
|
|
}
|
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() {
|
2018-07-20 03:51:18 +02:00
|
|
|
if (routing.isOn(Page.Terminal)) {
|
2016-11-24 23:30:33 +01:00
|
|
|
$('.terminal-input').focus();
|
|
|
|
}
|
|
|
|
});
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
$(document).keydown(function(e) {
|
2018-07-20 03:51:18 +02:00
|
|
|
if (routing.isOn(Page.Terminal)) {
|
|
|
|
if (e.which == KEY.CTRL) {
|
2016-12-19 21:59:13 +01:00
|
|
|
terminalCtrlPressed = true;
|
2018-05-06 22:27:47 +02:00
|
|
|
} else if (e.shiftKey) {
|
|
|
|
shiftKeyPressed = true;
|
2018-09-14 23:03:31 +02:00
|
|
|
} else if (terminalCtrlPressed || shiftKeyPressed || Terminal.contractOpen) {
|
2019-04-13 03:22:46 +02:00
|
|
|
// Don't focus
|
2016-12-19 21:59:13 +01:00
|
|
|
} 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
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
terminalCtrlPressed = false;
|
2018-05-06 22:27:47 +02:00
|
|
|
shiftKeyPressed = false;
|
2016-12-19 21:59:13 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
});
|
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
$(document).keyup(function(e) {
|
2018-07-20 03:51:18 +02:00
|
|
|
if (routing.isOn(Page.Terminal)) {
|
|
|
|
if (e.which == KEY.CTRL) {
|
2016-12-19 21:59:13 +01:00
|
|
|
terminalCtrlPressed = false;
|
|
|
|
}
|
2018-05-06 22:27:47 +02:00
|
|
|
if (e.shiftKey) {
|
|
|
|
shiftKeyPressed = false;
|
|
|
|
}
|
2016-11-24 23:30:33 +01: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,
|
|
|
|
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
|
|
|
|
2017-09-13 16:22:22 +02:00
|
|
|
resetTerminalInput: function() {
|
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>` +
|
2018-08-29 05:24:38 +02:00
|
|
|
'<textarea type="text" id="terminal-input-text-box" class="terminal-input" tabindex="1"/>';
|
|
|
|
|
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>` +
|
|
|
|
`<input type="text" id="terminal-input-text-box" class="terminal-input" tabindex="1"/>`;
|
2018-08-29 05:24:38 +02:00
|
|
|
}
|
2017-09-13 16:22:22 +02:00
|
|
|
var hdr = document.getElementById("terminal-input-header");
|
|
|
|
hdr.style.display = "inline";
|
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 end = terminalInput.selectionEnd;
|
|
|
|
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;
|
|
|
|
var end = terminalInput.selectionEnd;
|
|
|
|
|
|
|
|
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
|
2018-08-30 19:00:38 +02:00
|
|
|
Terminal.actionTime = calculateHackingTime(Player.getCurrentServer()) / 4;
|
|
|
|
Terminal.startAction();
|
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
|
|
|
} else if (Terminal.analyzeFlag) {
|
|
|
|
Terminal.finishAnalyze(cancelled);
|
|
|
|
}
|
2017-09-13 16:22:22 +02:00
|
|
|
},
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Complete the hack/analyze command
|
2016-12-21 17:33:00 +01:00
|
|
|
finishHack: function(cancelled = false) {
|
|
|
|
if (cancelled == false) {
|
2017-06-02 06:15:45 +02:00
|
|
|
var server = Player.getCurrentServer();
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Calculate whether hack was successful
|
2018-08-30 19:00:38 +02:00
|
|
|
var hackChance = calculateHackingChance(server);
|
2016-12-21 17:33:00 +01:00
|
|
|
var rand = Math.random();
|
2018-08-30 19:00:38 +02:00
|
|
|
var expGainedOnSuccess = calculateHackingExpGain(server);
|
2017-05-05 17:50:55 +02:00
|
|
|
var expGainedOnFailure = (expGainedOnSuccess / 4);
|
2019-04-13 03:22:46 +02:00
|
|
|
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);
|
2017-07-22 00:54:55 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-02 16:45:33 +02:00
|
|
|
server.manuallyHacked = true;
|
2018-08-30 19:00:38 +02:00
|
|
|
var moneyGained = calculatePercentMoneyHacked(server);
|
2017-06-02 06:15:45 +02:00
|
|
|
moneyGained = Math.floor(server.moneyAvailable * moneyGained);
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
if (moneyGained <= 0) {moneyGained = 0;} // Safety check
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2017-06-02 06:15:45 +02:00
|
|
|
server.moneyAvailable -= moneyGained;
|
2016-12-21 17:33:00 +01:00
|
|
|
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
|
|
|
|
2018-09-12 17:53:08 +02:00
|
|
|
post("Hack successful! Gained " + numeralWrapper.format(moneyGained, '($0,0.00)') + " and " + numeralWrapper.format(expGainedOnSuccess, '0.0000') + " hacking EXP");
|
2019-04-13 03:22:46 +02:00
|
|
|
} 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)
|
2018-09-12 17:53:08 +02:00
|
|
|
post("Failed to hack " + server.hostname + ". Gained " + numeralWrapper.format(expGainedOnFailure, '0.0000') + " hacking EXP");
|
2016-12-21 17:33:00 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal
|
2016-11-01 06:30:59 +01:00
|
|
|
$("#hack-progress-bar").attr('id', "old-hack-progress-bar");
|
|
|
|
$("#hack-progress").attr('id', "old-hack-progress");
|
2017-09-13 16:22:22 +02:00
|
|
|
Terminal.resetTerminalInput();
|
2017-07-25 03:06:40 +02:00
|
|
|
$('input[class=terminal-input]').prop('disabled', false);
|
2016-11-01 06:30:59 +01:00
|
|
|
|
|
|
|
Terminal.hackFlag = false;
|
2016-10-27 20:26:00 +02:00
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2016-12-21 17:33:00 +01:00
|
|
|
finishAnalyze: function(cancelled = false) {
|
|
|
|
if (cancelled == false) {
|
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;
|
2018-08-30 19:00:38 +02:00
|
|
|
post(currServ.hostname + ": ");
|
|
|
|
post("Organization name: " + currServ.organizationName);
|
2017-05-06 08:24:01 +02:00
|
|
|
var rootAccess = "";
|
2018-08-30 19:00:38 +02:00
|
|
|
if (currServ.hasAdminRights) {rootAccess = "YES";}
|
2017-05-06 08:24:01 +02:00
|
|
|
else {rootAccess = "NO";}
|
|
|
|
post("Root Access: " + rootAccess);
|
2019-03-25 04:03:24 +01:00
|
|
|
if (!isHacknet) { post("Required hacking skill: " + currServ.requiredHackingSkill); }
|
2018-09-12 17:53:08 +02:00
|
|
|
post("Server security level: " + numeralWrapper.format(currServ.hackDifficulty, '0.000a'));
|
2018-09-23 02:36:28 +02:00
|
|
|
post("Chance to hack: " + numeralWrapper.format(calculateHackingChance(currServ), '0.00%'));
|
2018-09-12 17:53:08 +02:00
|
|
|
post("Time to hack: " + numeralWrapper.format(calculateHackingTime(currServ), '0.000') + " seconds");
|
2018-09-23 02:36:28 +02:00
|
|
|
post("Total money available on server: " + numeralWrapper.format(currServ.moneyAvailable, '$0,0.00'));
|
2019-03-25 04:03:24 +01:00
|
|
|
if (!isHacknet) { post("Required number of open ports for NUKE: " + currServ.numOpenPortsRequired); }
|
2018-09-10 20:51:21 +02:00
|
|
|
|
2018-08-30 19:00:38 +02:00
|
|
|
if (currServ.sshPortOpen) {
|
2016-12-21 17:33:00 +01:00
|
|
|
post("SSH port: Open")
|
|
|
|
} else {
|
|
|
|
post("SSH port: Closed")
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-08-30 19:00:38 +02:00
|
|
|
if (currServ.ftpPortOpen) {
|
2016-12-21 17:33:00 +01:00
|
|
|
post("FTP port: Open")
|
|
|
|
} else {
|
|
|
|
post("FTP port: Closed")
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-08-30 19:00:38 +02:00
|
|
|
if (currServ.smtpPortOpen) {
|
2016-12-21 17:33:00 +01:00
|
|
|
post("SMTP port: Open")
|
|
|
|
} else {
|
|
|
|
post("SMTP port: Closed")
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-08-30 19:00:38 +02:00
|
|
|
if (currServ.httpPortOpen) {
|
2016-12-21 17:33:00 +01:00
|
|
|
post("HTTP port: Open")
|
|
|
|
} else {
|
|
|
|
post("HTTP port: Closed")
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2018-08-30 19:00:38 +02:00
|
|
|
if (currServ.sqlPortOpen) {
|
2016-12-21 17:33:00 +01:00
|
|
|
post("SQL port: Open")
|
|
|
|
} else {
|
|
|
|
post("SQL port: Closed")
|
|
|
|
}
|
|
|
|
}
|
2016-11-01 06:30:59 +01:00
|
|
|
Terminal.analyzeFlag = false;
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
// Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal
|
2016-11-21 07:11:14 +01:00
|
|
|
$("#hack-progress-bar").attr('id', "old-hack-progress-bar");
|
|
|
|
$("#hack-progress").attr('id', "old-hack-progress");
|
2017-09-13 16:22:22 +02:00
|
|
|
Terminal.resetTerminalInput();
|
2017-07-25 03:06:40 +02:00
|
|
|
$('input[class=terminal-input]').prop('disabled', false);
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
commands = commands.split(";");
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
executeCommand : function(command) {
|
2019-07-16 06:40:13 +02:00
|
|
|
if (Terminal.hackFlag || Terminal.analyzeFlag) {
|
|
|
|
postError(`Cannot execute command (${command}) while an action is in progress`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Process any aliases
|
2017-05-23 17:12:09 +02:00
|
|
|
command = substituteAliases(command);
|
2017-07-25 03:06:40 +02:00
|
|
|
|
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
|
2019-02-05 07:45:04 +01: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");}
|
2017-05-06 08:24:01 +02:00
|
|
|
break;
|
|
|
|
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();
|
2017-06-03 02:41:46 +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)) {
|
|
|
|
post(`Set global alias ${commandArray[1]}`);
|
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;
|
2016-10-20 20:26:38 +02:00
|
|
|
case "analyze":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
post("Incorrect usage of analyze command. Usage: analyze");
|
|
|
|
return;
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
2018-08-30 19:00:38 +02:00
|
|
|
Terminal.startAnalyze();
|
2016-10-20 20:26:38 +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]);
|
|
|
|
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;
|
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": {
|
|
|
|
if (commandArray.length !== 2) {
|
|
|
|
postError("Incorrect number of arguments. Usage: cd [dir]");
|
|
|
|
} else {
|
|
|
|
let dir = commandArray[1];
|
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
}
|
2016-10-20 20:26:38 +02:00
|
|
|
case "clear":
|
|
|
|
case "cls":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of clear/cls command. Usage: clear/cls");
|
|
|
|
return;
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
2016-10-20 23:11:01 +02:00
|
|
|
$("#terminal tr:not(:last)").remove();
|
|
|
|
postNetburnerText();
|
2017-07-25 03:06:40 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
case "connect": {
|
2019-04-13 03:22:46 +02:00
|
|
|
// 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");
|
2016-10-20 20:26:38 +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
|
|
|
}
|
2016-12-01 23:18:18 +01:00
|
|
|
case "free":
|
2017-05-06 21:12:45 +02:00
|
|
|
Terminal.executeFreeCommand(commandArray);
|
2016-10-20 20:26:38 +02:00
|
|
|
break;
|
2019-03-30 00:14:32 +01:00
|
|
|
case "hack": {
|
|
|
|
if (commandArray.length !== 1) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Incorrect usage of hack command. Usage: hack");
|
|
|
|
return;
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
2019-04-13 03:22:46 +02:00
|
|
|
// Hack the current PC (usually for money)
|
|
|
|
// You can't hack your home pc or servers you purchased
|
2019-03-30 00:14:32 +01:00
|
|
|
if (s.purchasedByPlayer) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Cannot hack your own machines! You are currently connected to your home PC or one of your purchased servers");
|
2019-03-30 00:14:32 +01:00
|
|
|
} else if (s.hasAdminRights == false ) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("You do not have admin rights for this machine! Cannot hack");
|
2019-03-30 00:14:32 +01:00
|
|
|
} else if (s.requiredHackingSkill > Player.hacking_skill) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Your hacking skill is not high enough to attempt hacking this machine. Try analyzing the machine to determine the required hacking skill");
|
2019-03-30 00:14:32 +01:00
|
|
|
} else if (s instanceof HacknetServer) {
|
|
|
|
postError("Cannot hack this type of Server")
|
|
|
|
} else {
|
2018-08-30 19:00:38 +02:00
|
|
|
Terminal.startHack();
|
2016-10-20 20:26:38 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-03-30 00:14:32 +01:00
|
|
|
}
|
2016-10-20 20:26:38 +02:00
|
|
|
case "help":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1 && commandArray.length !== 2) {
|
|
|
|
postError("Incorrect usage of help command. Usage: help");
|
|
|
|
return;
|
2016-12-15 18:51:23 +01:00
|
|
|
}
|
2019-02-05 07:45:04 +01: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);
|
|
|
|
}
|
2016-10-20 20:26:38 +02:00
|
|
|
break;
|
2016-12-14 00:52:32 +01:00
|
|
|
case "home":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
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();
|
2016-12-14 00:52:32 +01:00
|
|
|
break;
|
2016-10-20 20:26:38 +02:00
|
|
|
case "hostname":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of hostname command. Usage: hostname");
|
|
|
|
return;
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
2016-12-01 23:18:18 +01:00
|
|
|
post(Player.getCurrentServer().hostname);
|
2016-10-20 20:26:38 +02:00
|
|
|
break;
|
|
|
|
case "ifconfig":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of ifconfig command. Usage: ifconfig");
|
|
|
|
return;
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
2016-12-01 23:18:18 +01:00
|
|
|
post(Player.getCurrentServer().ip);
|
2016-10-20 20:26:38 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
case "kill": {
|
2019-02-05 07:45:04 +01:00
|
|
|
Terminal.executeKillCommand(commandArray);
|
2016-10-20 20:26:38 +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
|
|
|
}
|
|
|
|
case "ls": {
|
2017-05-06 08:24:01 +02:00
|
|
|
Terminal.executeListCommand(commandArray);
|
2016-10-20 20:26:38 +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;
|
|
|
|
}
|
2016-11-24 23:30:33 +01:00
|
|
|
case "nano":
|
2019-02-05 07:45:04 +01:00
|
|
|
Terminal.executeNanoCommand(commandArray);
|
2016-11-24 23:30:33 +01:00
|
|
|
break;
|
2016-10-20 20:26:38 +02:00
|
|
|
case "ps":
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of ps command. Usage: ps");
|
|
|
|
return;
|
2016-12-05 23:31:46 +01:00
|
|
|
}
|
2019-02-05 07:45:04 +01:00
|
|
|
for (let i = 0; i < s.runningScripts.length; i++) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
post(res);
|
2016-12-05 23:31:46 +01:00
|
|
|
}
|
2016-10-20 20:26:38 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
case "rm": {
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 2) {
|
|
|
|
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
|
2019-04-10 08:07:12 +02:00
|
|
|
let delTarget = Terminal.getFilepath(commandArray[1]);
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
const status = s.removeFile(delTarget);
|
|
|
|
if (!status.res) {
|
|
|
|
postError(status.msg);
|
2017-09-12 01:14:51 +02:00
|
|
|
}
|
2016-10-20 20:26:38 +02:00
|
|
|
break;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2016-10-20 20:26:38 +02:00
|
|
|
case "run":
|
2019-04-13 03:22:46 +02:00
|
|
|
// Run a program or a script
|
2019-02-06 03:32:15 +01:00
|
|
|
if (commandArray.length < 2) {
|
2019-02-05 07:45:04 +01:00
|
|
|
postError("Incorrect number of arguments. Usage: run [program/script] [-t] [num threads] [arg1] [arg2]...");
|
2016-10-24 23:16:51 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Check if its a script or just a program/executable
|
2019-02-06 03:32:15 +01:00
|
|
|
if (isScriptFilename(executableName)) {
|
|
|
|
Terminal.runScript(commandArray);
|
2018-09-14 23:03:31 +02:00
|
|
|
} else if (executableName.endsWith(".cct")) {
|
|
|
|
Terminal.runContract(executableName);
|
2018-05-06 22:27:47 +02:00
|
|
|
} else {
|
2019-02-20 09:42:27 +01:00
|
|
|
Terminal.runProgram(commandArray);
|
2016-10-24 23:16:51 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-20 20:26:38 +02:00
|
|
|
break;
|
2017-05-23 19:36:35 +02:00
|
|
|
case "scan":
|
|
|
|
Terminal.executeScanCommand(commandArray);
|
|
|
|
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 */
|
2016-10-20 20:26:38 +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;
|
2019-04-10 08:07:12 +02:00
|
|
|
case "tail": {
|
|
|
|
try {
|
|
|
|
if (commandArray.length < 2) {
|
|
|
|
postError("Incorrect number of arguments. Usage: tail [script] [arg1] [arg2]...");
|
|
|
|
} else {
|
|
|
|
const scriptName = Terminal.getFilepath(commandArray[1]);
|
|
|
|
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 script arguments
|
|
|
|
const args = [];
|
|
|
|
for (let 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
|
|
|
|
const runningScript = findRunningScript(scriptName, args, s);
|
|
|
|
if (runningScript == null) {
|
|
|
|
postError("No such script exists");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2016-12-02 22:57:20 +01: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
|
|
|
}
|
|
|
|
case "top": {
|
2019-02-05 07:45:04 +01:00
|
|
|
if (commandArray.length !== 1) {
|
|
|
|
postError("Incorrect usage of top command. Usage: top");
|
|
|
|
return;
|
2017-06-16 20:17:40 +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}`;
|
|
|
|
|
|
|
|
post(headers);
|
2017-06-16 19:23:42 +02:00
|
|
|
|
2019-02-05 07:45:04 +01:00
|
|
|
let currRunningScripts = s.runningScripts;
|
2019-04-13 03:22:46 +02:00
|
|
|
// Iterate through scripts on current server
|
2019-02-05 07:45:04 +01:00
|
|
|
for (let i = 0; i < currRunningScripts.length; i++) {
|
|
|
|
let script = currRunningScripts[i];
|
2017-06-16 16:57:03 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Calculate name padding
|
2019-06-27 09:01:06 +02:00
|
|
|
const numSpacesScript = Math.max(0, scriptWidth - script.filename.length);
|
|
|
|
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
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Calculate thread padding
|
2019-06-27 09:01:06 +02:00
|
|
|
const numSpacesThread = Math.max(0, threadsWidth - (script.threads + "").length);
|
|
|
|
const spacesThread = " ".repeat(numSpacesThread);
|
2017-06-16 16:57:03 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Calculate and transform RAM usage
|
2019-06-27 09:01:06 +02:00
|
|
|
const ramUsage = numeralWrapper.format(getRamUsageFromRunningScript(script) * script.threads, '0.00') + " GB";
|
|
|
|
|
|
|
|
const entry = [
|
|
|
|
script.filename,
|
|
|
|
spacesScript,
|
|
|
|
script.pid,
|
|
|
|
spacesPid,
|
|
|
|
script.threads,
|
|
|
|
spacesThread,
|
|
|
|
ramUsage
|
|
|
|
].join("");
|
|
|
|
post(entry);
|
2017-06-16 20:17:40 +02:00
|
|
|
}
|
2016-11-17 23:25:40 +01:00
|
|
|
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
|
|
|
}
|
2016-10-20 20:26:38 +02:00
|
|
|
default:
|
2019-02-05 07:45:04 +01:00
|
|
|
postError(`Command ${commandArray[0]} not found`);
|
2016-10-20 20:26:38 +02:00
|
|
|
}
|
|
|
|
},
|
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;
|
|
|
|
}
|
2019-03-29 17:26:25 +01:00
|
|
|
const ram = numeralWrapper.format(Player.getCurrentServer().maxRam, '0.00');
|
|
|
|
const used = numeralWrapper.format(Player.getCurrentServer().ramUsed, '0.00');
|
|
|
|
const avail = numeralWrapper.format(Player.getCurrentServer().maxRam - Player.getCurrentServer().ramUsed, '0.00');
|
|
|
|
const maxLength = Math.max(ram.length, Math.max(used.length, avail.length));
|
|
|
|
const usedPercent = numeralWrapper.format(Player.getCurrentServer().ramUsed/Player.getCurrentServer().maxRam*100, '0.00');
|
|
|
|
|
|
|
|
post(`Total: ${" ".repeat(maxLength-ram.length)}${ram} GB`);
|
|
|
|
post(`Used: ${" ".repeat(maxLength-used.length)}${used} GB (${usedPercent}%)`);
|
|
|
|
post(`Available: ${" ".repeat(maxLength-avail.length)}${avail} GB`);
|
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
|
2019-03-29 16:50:05 +01:00
|
|
|
let allFiles = [];
|
|
|
|
let folders = [];
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-03-29 16:50:05 +01:00
|
|
|
function handleFn(fn) {
|
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
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
allFiles.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();
|
|
|
|
for (const program of s.programs) handleFn(program);
|
|
|
|
for (const script of s.scripts) handleFn(script.filename);
|
|
|
|
for (const txt of s.textFiles) handleFn(txt.fn);
|
|
|
|
for (const contract of s.contracts) handleFn(contract.fn);
|
|
|
|
for (const msgOrLit of s.messages) (msgOrLit instanceof Message) ? handleFn(msgOrLit.filename) : handleFn(msgOrLit);
|
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
// Sort the files/folders alphabetically then print each
|
2017-05-06 08:24:01 +02:00
|
|
|
allFiles.sort();
|
2019-04-10 08:07:12 +02:00
|
|
|
folders.sort();
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
const config = { color: "#0000FF" };
|
|
|
|
for (const dir of folders) {
|
|
|
|
postContent(dir, config);
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-10 08:07:12 +02:00
|
|
|
for (const file of allFiles) {
|
|
|
|
postContent(file);
|
2019-02-05 07:45:04 +01:00
|
|
|
}
|
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 s = Player.getCurrentServer();
|
|
|
|
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;
|
|
|
|
|
|
|
|
post(`This script requires ${numeralWrapper.format(ramUsage, '0.00')} GB of RAM to run for ${numThreads} thread(s)`);
|
|
|
|
} 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 = ""
|
|
|
|
if(filename.endsWith(".ns")) {
|
|
|
|
code = `export async function main(ns) {
|
|
|
|
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
Engine.loadScriptEditorContent(filepath, code);
|
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); }
|
2017-05-24 23:35:24 +02:00
|
|
|
post(dashes + "RAM: " + 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 ip = destServer.ip;
|
|
|
|
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
|
|
|
|
2019-04-13 03:22:46 +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
|
2019-02-20 09:42:27 +01:00
|
|
|
runProgram: function(commandArray) {
|
|
|
|
if (commandArray.length < 2) { return; }
|
|
|
|
|
|
|
|
// Check if you have the program on your computer. If you do, execute it, otherwise
|
|
|
|
// display an error message
|
|
|
|
const programName = commandArray[1];
|
|
|
|
|
|
|
|
if (Player.hasProgram(programName)) {
|
|
|
|
Terminal.executeProgram(commandArray);
|
2017-06-23 16:23:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-02-16 19:52:11 +01:00
|
|
|
post("ERROR: No such executable on home computer (Only programs that exist on your home computer can be run)");
|
2016-10-27 20:26:00 +02:00
|
|
|
},
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Contains the implementations of all possible programs
|
2019-02-20 09:42:27 +01:00
|
|
|
executeProgram: function(commandArray) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
post(targetServer.hostname + ":");
|
|
|
|
post("Server base security level: " + targetServer.baseDifficulty);
|
|
|
|
post("Server current security level: " + targetServer.hackDifficulty);
|
|
|
|
post("Server growth rate: " + targetServer.serverGrowth);
|
2018-09-14 23:03:31 +02:00
|
|
|
post("Netscript hack() execution time: " + numeralWrapper.format(calculateHackingTime(targetServer), '0.0') + "s");
|
|
|
|
post("Netscript grow() execution time: " + numeralWrapper.format(calculateGrowTime(targetServer), '0.0') + "s");
|
|
|
|
post("Netscript weaken() execution time: " + numeralWrapper.format(calculateWeakenTime(targetServer), '0.0') + "s");
|
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) &&
|
|
|
|
((Player.hacking_skill >= 2500)||
|
|
|
|
(Player.strength >= 1500 &&
|
|
|
|
Player.defense >= 1500 &&
|
|
|
|
Player.dexterity >= 1500 &&
|
|
|
|
Player.agility >= 1500));
|
|
|
|
if(!fulfilled) {
|
2019-03-29 17:10:22 +01:00
|
|
|
post(`Augmentations: ${Player.augmentations.length} / ${numAugReq}`);
|
2018-07-31 06:18:38 +02:00
|
|
|
|
2019-03-29 17:11:01 +01:00
|
|
|
post(`Money: ${numeralWrapper.format(Player.money.toNumber(), '($0.000a)')} / ${numeralWrapper.format(1e11, '($0.000a)')}`);
|
2018-07-31 06:18:38 +02:00
|
|
|
post("One path below must be fulfilled...");
|
|
|
|
post("----------HACKING PATH----------");
|
2019-03-29 17:10:22 +01:00
|
|
|
post(`Hacking skill: ${Player.hacking_skill} / 2500`);
|
2018-07-31 06:18:38 +02:00
|
|
|
post("----------COMBAT PATH----------");
|
2019-03-29 17:10:22 +01:00
|
|
|
post(`Strength: ${Player.strength} / 1500`);
|
|
|
|
post(`Defense: ${Player.defense} / 1500`);
|
|
|
|
post(`Dexterity: ${Player.dexterity} / 1500`);
|
|
|
|
post(`Agility: ${Player.agility} / 1500`);
|
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);
|
2016-10-24 23:16:51 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-06 03:32:15 +01:00
|
|
|
runScript: function(commandArray) {
|
|
|
|
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
|
|
|
|
|
|
|
const server = Player.getCurrentServer();
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-04-13 03:22:46 +02:00
|
|
|
// Check if the script exists and if it does run it
|
2016-12-14 21:29:40 +01:00
|
|
|
for (var i = 0; i < server.scripts.length; i++) {
|
2019-04-10 08:07:12 +02:00
|
|
|
if (server.scripts[i].filename === scriptName) {
|
2019-04-13 03:22:46 +02:00
|
|
|
// 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];
|
2018-07-03 05:35:12 +02:00
|
|
|
var ramUsage = script.ramUsage * numThreads;
|
2016-12-14 21:29:40 +01:00
|
|
|
var ramAvailable = server.maxRam - server.ramUsed;
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2016-12-14 21:29:40 +01:00
|
|
|
if (server.hasAdminRights == false) {
|
2016-11-28 23:02:06 +01:00
|
|
|
post("Need root access to run script");
|
2016-12-14 00:52:32 +01:00
|
|
|
return;
|
2016-12-14 21:29:40 +01:00
|
|
|
} else if (ramUsage > ramAvailable){
|
2017-06-11 03:46:02 +02:00
|
|
|
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");
|
2016-12-14 21:29:40 +01:00
|
|
|
return;
|
2017-06-17 04:53:57 +02:00
|
|
|
} else {
|
2019-04-13 03:22:46 +02:00
|
|
|
// 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)) {
|
|
|
|
post("Running script with " + numThreads + " thread(s) and args: " + arrayToString(args) + ".");
|
|
|
|
} else {
|
|
|
|
postError(`Failed to start script`);
|
|
|
|
}
|
|
|
|
return;
|
2016-11-28 23:02:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2016-11-28 23:02:06 +01: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
|
|
|
|
2018-07-18 20:34:18 +02:00
|
|
|
export {postNetburnerText, Terminal};
|