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:
David Walker
2026-02-15 10:29:47 -08:00
committed by GitHub
parent b5ab495837
commit b51ed8fd59
50 changed files with 478 additions and 471 deletions

View File

@@ -76,5 +76,5 @@ RAM cost: 2 GB
Generate a dummy contract on the current server with no reward. Used to test various algorithms.
This function will return null and not generate a contract if the randomized contract name is the same as another contract's name.
This function will return null and not generate a contract if the randomized contract name is the same as another contract's name or the host is offline.

View File

@@ -9,7 +9,7 @@ Runs BruteSSH.exe on a server.
**Signature:**
```typescript
brutessh(host: string): boolean;
brutessh(host?: string): boolean;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Given a hostname, returns its IP address; or given an IP address, returns its ho
**Signature:**
```typescript
dnsLookup(host: string): string;
dnsLookup(host?: string): string;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -58,7 +58,7 @@ string
</td><td>
Hostname/IP of the `target server` on which to execute the script.
Hostname/IP of the target server on which to execute the script.
</td></tr>

View File

@@ -9,7 +9,7 @@ Runs FTPCrack.exe on a server.
**Signature:**
```typescript
ftpcrack(host: string): boolean;
ftpcrack(host?: string): boolean;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the execution time of a grow() call.
**Signature:**
```typescript
getGrowTime(host: string): number;
getGrowTime(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the execution time of a hack() call.
**Signature:**
```typescript
getHackTime(host: string): number;
getHackTime(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the exp gain of a script.
**Signature:**
```typescript
getScriptExpGain(script: string, host: string, ...args: ScriptArg[]): number;
getScriptExpGain(script: string, host?: string, ...args: ScriptArg[]): number;
```
## Parameters
@@ -58,7 +58,7 @@ string
</td><td>
Hostname/IP of the server on which script is running.
_(Optional)_ Hostname/IP of the server on which the script is running. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the income of a script.
**Signature:**
```typescript
getScriptIncome(script: string, host: string, ...args: ScriptArg[]): number;
getScriptIncome(script: string, host?: string, ...args: ScriptArg[]): number;
```
## Parameters
@@ -58,7 +58,7 @@ string
</td><td>
Hostname/IP of the server on which script is running.
_(Optional)_ Hostname/IP of the server on which the script is running. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the base security level of a server.
**Signature:**
```typescript
getServerBaseSecurityLevel(host: string): number;
getServerBaseSecurityLevel(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get a server growth parameter.
**Signature:**
```typescript
getServerGrowth(host: string): number;
getServerGrowth(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the maximum money available on a server.
**Signature:**
```typescript
getServerMaxMoney(host: string): number;
getServerMaxMoney(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the maximum amount of RAM on a server.
**Signature:**
```typescript
getServerMaxRam(host: string): number;
getServerMaxRam(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Returns the minimum security level of the target server.
**Signature:**
```typescript
getServerMinSecurityLevel(host: string): number;
getServerMinSecurityLevel(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get money available on a server.
**Signature:**
```typescript
getServerMoneyAvailable(host: string): number;
getServerMoneyAvailable(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Returns the number of open ports required to successfully run NUKE.exe on the sp
**Signature:**
```typescript
getServerNumPortsRequired(host: string): number;
getServerNumPortsRequired(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Returns the required hacking level of the target server.
**Signature:**
```typescript
getServerRequiredHackingLevel(host: string): number;
getServerRequiredHackingLevel(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get server security level.
**Signature:**
```typescript
getServerSecurityLevel(host: string): number;
getServerSecurityLevel(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the used RAM on a server.
**Signature:**
```typescript
getServerUsedRam(host: string): number;
getServerUsedRam(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the execution time of a weaken() call.
**Signature:**
```typescript
getWeakenTime(host: string): number;
getWeakenTime(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Spoof money in a server's bank account, increasing the amount available.
**Signature:**
```typescript
grow(host: string, opts?: BasicHGWOptions): Promise<number>;
grow(host?: string, opts?: BasicHGWOptions): Promise<number>;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server to grow.
_(Optional)_ Hostname/IP of the target server to grow. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Steal a server's money.
**Signature:**
```typescript
hack(host: string, opts?: BasicHGWOptions): Promise<number>;
hack(host?: string, opts?: BasicHGWOptions): Promise<number>;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server to hack.
_(Optional)_ Hostname/IP of the target server to hack. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Get the chance of successfully hacking a server.
**Signature:**
```typescript
hackAnalyzeChance(host: string): number;
hackAnalyzeChance(host?: string): number;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -58,7 +58,7 @@ string
</td><td>
_(Optional)_ Hostname/IP of the target server. The number of threads is limited to the number needed to hack the server's maximum amount of money.
_(Optional)_ Hostname/IP of the target server. Optional. If unspecified, the threads are not capped.
</td></tr>
@@ -74,5 +74,5 @@ The security increase.
RAM cost: 1 GB
Returns the security increase that would occur if a hack with this many threads happened.
Returns the security increase that would occur if a hack with this many threads happened. The number of threads is limited to the number needed to hack the server's maximum amount of money.

View File

@@ -42,7 +42,7 @@ string
</td><td>
Hostname of the target server to analyze.
Hostname/IP of the target server to analyze.
</td></tr>

View File

@@ -9,7 +9,7 @@ Check if you have root access on a server.
**Signature:**
```typescript
hasRootAccess(host: string): boolean;
hasRootAccess(host?: string): boolean;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Runs HTTPWorm.exe on a server.
**Signature:**
```typescript
httpworm(host: string): boolean;
httpworm(host?: string): boolean;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -42,7 +42,7 @@ string
</td><td>
_(Optional)_ Hostname/IP of the server on which to kill all scripts.
_(Optional)_ Hostname/IP of the server on which to kill all scripts. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Runs NUKE.exe on a server.
**Signature:**
```typescript
nuke(host: string): boolean;
nuke(host?: string): boolean;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Runs relaySMTP.exe on a server.
**Signature:**
```typescript
relaysmtp(host: string): boolean;
relaysmtp(host?: string): boolean;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Kill all scripts with a filename.
**Signature:**
```typescript
scriptKill(script: string, host: string): boolean;
scriptKill(script: string, host?: string): boolean;
```
## Parameters
@@ -58,7 +58,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Check if any script with a filename is running.
**Signature:**
```typescript
scriptRunning(script: string, host: string): boolean;
scriptRunning(script: string, host?: string): boolean;
```
## Parameters
@@ -58,7 +58,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Returns a boolean denoting whether or not the specified server exists.
**Signature:**
```typescript
serverExists(host: string): boolean;
serverExists(host?: string): boolean;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Runs SQLInject.exe on a server.
**Signature:**
```typescript
sqlinject(host: string): boolean;
sqlinject(host?: string): boolean;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server.
_(Optional)_ Hostname/IP of the target server. Optional. Defaults to current server if not provided.
</td></tr>

View File

@@ -9,7 +9,7 @@ Reduce a server's security level.
**Signature:**
```typescript
weaken(host: string, opts?: BasicHGWOptions): Promise<number>;
weaken(host?: string, opts?: BasicHGWOptions): Promise<number>;
```
## Parameters
@@ -42,7 +42,7 @@ string
</td><td>
Hostname/IP of the target server to weaken.
_(Optional)_ Hostname/IP of the target server to weaken. Optional. Defaults to current server if not provided.
</td></tr>