DNET: Add JS object properties as server names; refactor save/load/server storage to support this (#2482)

This commit is contained in:
Michael Ficocelli
2026-02-10 03:13:47 -05:00
committed by GitHub
parent b99e522d1c
commit bab6280735
14 changed files with 213 additions and 120 deletions

View File

@@ -77,7 +77,7 @@ afterEach(() => {
});
const getLatestResponseTime = (server: DarknetServer) => {
const lastPasswordResponse = DarknetState.serverState[server.hostname].serverLogs[0].message;
const lastPasswordResponse = getServerLogs(server, 1, true)[0].message;
assertPasswordResponse(lastPasswordResponse);
assertString(lastPasswordResponse.data);
const timeMatch = lastPasswordResponse.data.match(/Response time: (\d+\.?\d*)ms/);
@@ -383,7 +383,7 @@ describe("Password Tests", () => {
expect(response1.message).toBe(GenericResponseMessage.AuthFailure);
const serverLogs = DarknetState.serverState[server.hostname].serverLogs;
const serverLogs = getServerLogs(server, 5, true);
expect(serverLogs.length).toBe(5);
assertPasswordResponse(serverLogs[4].message);
assertPasswordResponse(serverLogs[3].message);

View File

@@ -81,7 +81,11 @@ function getNsOnServerNearLabyrinth() {
function getFirstDarknetServerAdjacentToDarkWeb() {
addLowLevelServersIfNeeded();
const darkweb = getDarknetServerOrThrow(SpecialServers.DarkWeb);
return darkweb.serversOnNetwork.filter((hostname) => hostname !== SpecialServers.Home)[0];
const result = darkweb.serversOnNetwork.filter((hostname) => hostname !== SpecialServers.Home)[0];
if (!result) {
throw new Error("No darknet server adjacent to darkweb found");
}
return result;
}
function getNsOnNonDarkwebDarknetServer() {

View File

@@ -23,6 +23,8 @@ fixDoImportIssue();
//
// Most of the Servers have been removed to reduce space. Default values have
// been removed both for space, and to test that they are added correctly.
// The one remaining server has been renamed to "__proto__" to test the
// handling of darknet servers with unusual hostnames.
function loadStandardServers() {
loadAllServers(String.raw`{
"home": {
@@ -128,15 +130,15 @@ function loadStandardServers() {
]
},
"serversOnNetwork": [
"n00dles"
"__proto__"
],
"purchasedByPlayer": true
}
},
"n00dles": {
"__proto__": {
"ctor": "Server",
"data": {
"hostname": "n00dles",
"hostname": "__proto__",
"ip": "61.6.6.2",
"maxRam": 4,
"organizationName": "Noodle Bar",

View File

@@ -0,0 +1,45 @@
import { RunningScript } from "../../../src/Script/RunningScript";
import { Reviver } from "../../../src/utils/GenericReviver";
import { Script } from "../../../src/Script/Script";
import { ScriptFilePath } from "../../../src/Paths/ScriptFilePath";
describe("Validate that a RunningScript can be saved and loaded", () => {
it("Save and Load", function () {
const hostname = "__proto__";
const filename = "test.js" as ScriptFilePath;
const args = ["arg1", "arg2"];
const ramUsage = 1.7;
const script = new Script(filename, "", hostname);
const runningScript = new RunningScript(script, ramUsage, args);
runningScript.dataMap.set(hostname, [1000, 2, 3, 4]);
const json = JSON.stringify(runningScript);
const revivedRunningScript = JSON.parse(json, Reviver) as RunningScript;
expect(revivedRunningScript).toBeInstanceOf(RunningScript);
expect(revivedRunningScript.filename).toEqual(filename);
expect(revivedRunningScript.server).toEqual(hostname);
expect(revivedRunningScript.args).toEqual(args);
expect(revivedRunningScript.ramUsage).toEqual(ramUsage);
expect(revivedRunningScript.dataMap.get(hostname)).toEqual([1000, 2, 3, 4]);
});
it("Loads a standard savefile shape", () => {
const data = ` {"ctor":"RunningScript","data":{"args":["arg1","arg2"],"dataMap":{"testserver":[1000,2,3,4]},"filename":"test.js","offlineExpGained":0,"offlineMoneyMade":0,"offlineRunningTime":0.01,"onlineExpGained":0,"onlineMoneyMade":0,"onlineRunningTime":0.01,"ramUsage":1.7,"server":"testserver","scriptKey":"test.js*[\\"arg1\\",\\"arg2\\"]","stdin":null,"tailOutputPipeConfig":null,"terminalOutputPipeConfig":null,"title":"test.js arg1 arg2","threads":1,"temporary":false}}`;
const hostname = "testserver";
const filename = "test.js" as ScriptFilePath;
const args = ["arg1", "arg2"];
const ramUsage = 1.7;
const revivedRunningScript = JSON.parse(data, Reviver) as RunningScript;
expect(revivedRunningScript).toBeInstanceOf(RunningScript);
expect(revivedRunningScript.filename).toEqual(filename);
expect(revivedRunningScript.server).toEqual(hostname);
expect(revivedRunningScript.ramUsage).toEqual(ramUsage);
expect(revivedRunningScript.args).toEqual(args);
expect(revivedRunningScript.dataMap.get(hostname)).toEqual([1000, 2, 3, 4]);
});
});

View File

@@ -0,0 +1,37 @@
import {
AddToAllServers,
GetAllServers,
loadAllServers,
prestigeAllServers,
saveAllServers,
} from "../../../src/Server/AllServers";
import { Server } from "../../../src/Server/Server";
import { IPAddress } from "../../../src/Types/strings";
describe("AllServers can be saved and loaded", () => {
it("saves and loads servers correctly", () => {
prestigeAllServers();
expect(GetAllServers(true)).toEqual([]);
const hostname = "__proto__";
const server1 = new Server({
hostname,
ip: "173.78.146.183" as IPAddress,
});
AddToAllServers(server1);
expect(GetAllServers(true)).toEqual([server1]);
const serializedServers = saveAllServers();
expect(serializedServers).toEqual(
`{"__proto__":{"ctor":"Server","data":{"contracts":[],"cpuCores":1,"ftpPortOpen":false,"hasAdminRights":false,"hostname":"__proto__","httpPortOpen":false,"ip":"173.78.146.183","isConnectedTo":false,"maxRam":0,"messages":[],"organizationName":"","programs":[],"scripts":{"ctor":"JSONMap","data":[]},"serversOnNetwork":[],"smtpPortOpen":false,"sqlPortOpen":false,"sshPortOpen":false,"textFiles":{"ctor":"JSONMap","data":[]},"purchasedByPlayer":false,"backdoorInstalled":false,"baseDifficulty":1,"hackDifficulty":1,"minDifficulty":1,"moneyAvailable":0,"moneyMax":0,"numOpenPortsRequired":5,"openPortCount":0,"requiredHackingSkill":1,"serverGrowth":1,"runningScripts":[]}}}`,
);
loadAllServers(serializedServers);
const loadedServers = GetAllServers(true);
expect(loadedServers.length).toEqual(1);
const loadedServer = loadedServers[0];
expect(loadedServer).toBeInstanceOf(Server);
expect(loadedServer.hostname).toEqual(server1.hostname);
expect(loadedServer.ip).toEqual(server1.ip);
expect(loadedServer.numOpenPortsRequired).toEqual(server1.numOpenPortsRequired);
});
});

View File

@@ -46,7 +46,7 @@ exports[`load/saveAllServers 1`] = `
]
},
"serversOnNetwork": [
"n00dles"
"__proto__"
],
"smtpPortOpen": false,
"sqlPortOpen": false,
@@ -90,14 +90,14 @@ exports[`load/saveAllServers 1`] = `
]
}
},
"n00dles": {
"__proto__": {
"ctor": "Server",
"data": {
"contracts": [],
"cpuCores": 1,
"ftpPortOpen": false,
"hasAdminRights": false,
"hostname": "n00dles",
"hostname": "__proto__",
"httpPortOpen": false,
"ip": "61.6.6.2",
"isConnectedTo": false,
@@ -182,7 +182,7 @@ exports[`load/saveAllServers pruning RunningScripts 1`] = `
]
},
"serversOnNetwork": [
"n00dles"
"__proto__"
],
"smtpPortOpen": false,
"sqlPortOpen": false,
@@ -205,14 +205,14 @@ exports[`load/saveAllServers pruning RunningScripts 1`] = `
"runningScripts": []
}
},
"n00dles": {
"__proto__": {
"ctor": "Server",
"data": {
"contracts": [],
"cpuCores": 1,
"ftpPortOpen": false,
"hasAdminRights": false,
"hostname": "n00dles",
"hostname": "__proto__",
"httpPortOpen": false,
"ip": "61.6.6.2",
"isConnectedTo": false,