Merge pull request #407 from kopelli/refactor-programs-from-terminal

Refactor - Determine program execution from terminal via hashmap instead switch statement
This commit is contained in:
danielyxie 2018-08-02 10:57:26 -04:00 committed by GitHub
commit b5e8d61ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1827,97 +1827,116 @@ let Terminal = {
if (splitArgs.length > 1) { if (splitArgs.length > 1) {
programName = splitArgs[0]; programName = splitArgs[0];
} }
switch (programName) {
case Programs.NukeProgram.name: // TODO: refactor this/these out of Terminal. This logic could reside closer to the Programs themselves.
if (s.hasAdminRights) { /**
* @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}
*/
/**
* @type {Object.<string, ProgramHandler}
*/
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"); post("You already have root access to this computer. There is no reason to run NUKE.exe");
} else { return;
if (s.openPortCount >= Player.getCurrentServer().numOpenPortsRequired) { }
s.hasAdminRights = true;
if (server.openPortCount >= Player.getCurrentServer().numOpenPortsRequired) {
server.hasAdminRights = true;
post("NUKE successful! Gained root access to " + Player.getCurrentServer().hostname); post("NUKE successful! Gained root access to " + Player.getCurrentServer().hostname);
//TODO Make this take time rather than be instant // TODO: Make this take time rather than be instant
} else { return;
}
post("NUKE unsuccessful. Not enough ports have been opened"); post("NUKE unsuccessful. Not enough ports have been opened");
} };
} programHandlers[Programs.BruteSSHProgram.name] = (server) => {
break; if (server.sshPortOpen) {
case Programs.BruteSSHProgram.name:
if (s.sshPortOpen) {
post("SSH Port (22) is already open!"); post("SSH Port (22) is already open!");
} else { return;
s.sshPortOpen = true; }
server.sshPortOpen = true;
post("Opened SSH Port(22)!") post("Opened SSH Port(22)!")
++s.openPortCount; server.openPortCount++;
} };
break; programHandlers[Programs.FTPCrackProgram.name] = (server) => {
case Programs.FTPCrackProgram.name: if (server.ftpPortOpen) {
if (s.ftpPortOpen) {
post("FTP Port (21) is already open!"); post("FTP Port (21) is already open!");
} else { return;
s.ftpPortOpen = true; }
server.ftpPortOpen = true;
post("Opened FTP Port (21)!"); post("Opened FTP Port (21)!");
++s.openPortCount; server.openPortCount++;
} };
break; programHandlers[Programs.RelaySMTPProgram.name] = (server) => {
case Programs.RelaySMTPProgram.name: if (server.smtpPortOpen) {
if (s.smtpPortOpen) {
post("SMTP Port (25) is already open!"); post("SMTP Port (25) is already open!");
} else { return;
s.smtpPortOpen = true; }
server.smtpPortOpen = true;
post("Opened SMTP Port (25)!"); post("Opened SMTP Port (25)!");
++s.openPortCount; server.openPortCount++;
} };
break; programHandlers[Programs.HTTPWormProgram.name] = (server) => {
case Programs.HTTPWormProgram.name: if (serv.httpPortOpen) {
if (s.httpPortOpen) {
post("HTTP Port (80) is already open!"); post("HTTP Port (80) is already open!");
} else { return;
s.httpPortOpen = true; }
server.httpPortOpen = true;
post("Opened HTTP Port (80)!"); post("Opened HTTP Port (80)!");
++s.openPortCount; server.openPortCount++;
} };
break; programHandlers[Programs.SQLInjectProgram.name] = (server) => {
case Programs.SQLInjectProgram.name: if (server.sqlPortOpen) {
if (s.sqlPortOpen) {
post("SQL Port (1433) is already open!"); post("SQL Port (1433) is already open!");
} else { return;
s.sqlPortOpen = true;
post("Opened SQL Port (1433)!");
++s.openPortCount;
} }
break;
case Programs.ServerProfiler.name: server.sqlPortOpen = true;
if (splitArgs.length != 2) { 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"); post("Must pass a server hostname or IP as an argument for ServerProfiler.exe");
return; return;
} }
var serv = getServer(splitArgs[1]);
if (serv == null) { const targetServer = getServer(args[1]);
if (targetServer == null) {
post("Invalid server IP/hostname"); post("Invalid server IP/hostname");
return; return;
} }
post(serv.hostname + ":"); post(targetServer.hostname + ":");
post("Server base security level: " + serv.baseDifficulty); post("Server base security level: " + targetServer.baseDifficulty);
post("Server current security level: " + serv.hackDifficulty); post("Server current security level: " + targetServer.hackDifficulty);
post("Server growth rate: " + serv.serverGrowth); post("Server growth rate: " + targetServer.serverGrowth);
post("Netscript hack() execution time: " + formatNumber(scriptCalculateHackingTime(serv), 1) + "s"); post("Netscript hack() execution time: " + formatNumber(scriptCalculateHackingTime(targetServer), 1) + "s");
post("Netscript grow() execution time: " + formatNumber(scriptCalculateGrowTime(serv)/1000, 1) + "s"); post("Netscript grow() execution time: " + formatNumber(scriptCalculateGrowTime(targetServer)/1000, 1) + "s");
post("Netscript weaken() execution time: " + formatNumber(scriptCalculateWeakenTime(serv)/1000, 1) + "s"); post("Netscript weaken() execution time: " + formatNumber(scriptCalculateWeakenTime(targetServer)/1000, 1) + "s");
break; };
case Programs.AutoLink.name: programHandlers[Programs.AutoLink.name] = () => {
post("This executable cannot be run."); post("This executable cannot be run.");
post("AutoLink.exe lets you automatically connect to other servers when using 'scan-analyze'."); 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."); post("When using scan-analyze, click on a server's hostname to connect to it.");
break; };
case Programs.DeepscanV1.name: programHandlers[Programs.DeepscanV1.name] = () => {
post("This executable cannot be run."); post("This executable cannot be run.");
post("DeepscanV1.exe lets you run 'scan-analyze' with a depth up to 5."); post("DeepscanV1.exe lets you run 'scan-analyze' with a depth up to 5.");
break; };
case Programs.DeepscanV2.name: programHandlers[Programs.DeepscanV2.name] = () => {
post("This executable cannot be run."); post("This executable cannot be run.");
post("DeepscanV2.exe lets you run 'scan-analyze' with a depth up to 10."); post("DeepscanV2.exe lets you run 'scan-analyze' with a depth up to 10.");
break; };
case Programs.Flight.name: programHandlers[Programs.Flight.name] = () => {
const fulfilled = Player.augmentations.length >= 30 && const fulfilled = Player.augmentations.length >= 30 &&
Player.money.gt(1e11) && Player.money.gt(1e11) &&
((Player.hacking_skill >= 2500)|| ((Player.hacking_skill >= 2500)||
@ -1937,14 +1956,15 @@ let Terminal = {
post("Defense: " + Player.defense + " / 1500"); post("Defense: " + Player.defense + " / 1500");
post("Dexterity: " + Player.dexterity + " / 1500"); post("Dexterity: " + Player.dexterity + " / 1500");
post("Agility: " + Player.agility + " / 1500"); post("Agility: " + Player.agility + " / 1500");
} else { return;
}
post("We will contact you."); post("We will contact you.");
post("-- Daedalus --"); post("-- Daedalus --");
} };
break; programHandlers[Programs.BitFlume.name] = () => {
case Programs.BitFlume.name: const yesBtn = yesNoBoxGetYesButton();
var yesBtn = yesNoBoxGetYesButton(), const noBtn = yesNoBoxGetNoButton();
noBtn = yesNoBoxGetNoButton();
yesBtn.innerHTML = "Travel to BitNode Nexus"; yesBtn.innerHTML = "Travel to BitNode Nexus";
noBtn.innerHTML = "Cancel"; noBtn.innerHTML = "Cancel";
yesBtn.addEventListener("click", function() { yesBtn.addEventListener("click", function() {
@ -1957,12 +1977,14 @@ let Terminal = {
yesNoBoxCreate("WARNING: USING THIS PROGRAM WILL CAUSE YOU TO LOSE ALL OF YOUR PROGRESS ON THE CURRENT BITNODE.<br><br>" + 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 " + "Do you want to travel to the BitNode Nexus? This allows you to reset the current BitNode " +
"and select a new one."); "and select a new one.");
};
break; if (!programHandlers.hasOwnProperty(programName)){
default:
post("Invalid executable. Cannot be run"); post("Invalid executable. Cannot be run");
return; return;
} }
programHandlers[programName](s, splitArgs);
}, },
runScript: function(scriptName) { runScript: function(scriptName) {