mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-25 18:50:56 +02:00
BUG: Fix missed cases in offline server handling (#2495)
There were two large holes in the existing offline server handling: 1. It didn't include IPs, so scripts that used IPs instead of hostnames would get exceptions thrown for "server not found." 2. Coverage was very low for non-Darknet APIs. Maybe most of them don't need to be covered, but many obvious ones like "ps", "killall" and "hasRootAccess" were missing. IMO the only reliable answer is one that enforces *all* are covered via the type system. To accomplish the second part, helpers.getServer() was changed to return null when a server is offline. This intentionally breaks a lot of its utility, which was to return a server unconditionally. To compensate, its utility was increased - it now also does unknown argument processing, allowing it to subsume a common line that all callers were repeating. Some callers switched to ctx.workerScript.getServer(), because they didn't actually need to be using helpers.getServer(). Similarly, a few callsites switched to GetServerOrThrow(), for the cases where it should be guaranteed that the server is valid. The rest are returning a default/failure response when the server is offline. (Except for contracts, which threw on failure already anyway.)
This commit is contained in:
@@ -16,6 +16,7 @@ import { DarknetState } from "./DarknetState";
|
||||
import { getRamBlock } from "../effects/ramblock";
|
||||
import { hasFullDarknetAccess } from "../effects/effects";
|
||||
import { getFriendlyType, TypeAssertionError } from "../../utils/TypeAssertion";
|
||||
import { isIPAddress } from "../../Types/strings";
|
||||
|
||||
export type PasswordResponse = {
|
||||
code: DarknetResponseCode;
|
||||
@@ -86,23 +87,43 @@ export const DnetServerBuilder = (options: DarknetServerOptions): DarknetServer
|
||||
isStationary: false,
|
||||
});
|
||||
server.updateRamUsed(ramBlock);
|
||||
removeFromOfflineServers(name);
|
||||
DarknetState.offlineServers.delete(name);
|
||||
DarknetState.offlineServers.delete(server.ip);
|
||||
AddToAllServers(server);
|
||||
|
||||
return server;
|
||||
};
|
||||
|
||||
export const generateDarknetServerName = (): string => {
|
||||
if (Math.random() < 0.03 && DarknetState.offlineServers.length > 0 && hasFullDarknetAccess()) {
|
||||
return DarknetState.offlineServers[Math.floor(Math.random() * DarknetState.offlineServers.length)];
|
||||
if (Math.random() < 0.03 && DarknetState.offlineServers.size > 0 && hasFullDarknetAccess()) {
|
||||
// Reuse a hostname that went offline. Note that we're only reusing hostnames,
|
||||
// not IPs. This asymmetry is intentional - people find hostnames easier to
|
||||
// work with, and this adds a bit of friction to compensate.
|
||||
|
||||
// Use an iterator to directly skip to the appropriate position. Sets don't
|
||||
// have a way to index by offset, and converting to an array would be wasteful.
|
||||
const offset = Math.floor(Math.random() * DarknetState.offlineServers.size);
|
||||
const it = DarknetState.offlineServers.values();
|
||||
for (let i = 0; i < offset; ++i) {
|
||||
it.next();
|
||||
}
|
||||
// The set contains both IPs and hostnames. If we hit an IP, keep going
|
||||
// forward until we find a hostname. *If* the Set implements traversal in
|
||||
// the same order as insertion, then the fact that we insert IPs first
|
||||
// means that we will always find a hostname and the sampling will be
|
||||
// unbiased. Otherwise, it will be Mostly Unbiased(TM), and in rare cases
|
||||
// we will fall off the end without reusing a name.
|
||||
let serverName = it.next().value;
|
||||
while (serverName != null && isIPAddress(serverName)) {
|
||||
serverName = it.next().value;
|
||||
}
|
||||
if (serverName != null) {
|
||||
return serverName;
|
||||
}
|
||||
}
|
||||
return decorateName(getBaseName());
|
||||
};
|
||||
|
||||
export const removeFromOfflineServers = (hostname: string): void => {
|
||||
DarknetState.offlineServers = DarknetState.offlineServers.filter((server) => server !== hostname);
|
||||
};
|
||||
|
||||
const getBaseName = (): string => {
|
||||
if (Math.random() < 0.05) {
|
||||
return commonPasswordDictionary[Math.floor(Math.random() * commonPasswordDictionary.length)];
|
||||
|
||||
Reference in New Issue
Block a user