BUGFIX: Prevent renaming servers to hacknet. (#1112)

* Add hacknet check to renaming
* Handle manual server purchases
* Handle automated server purchases
* Remove unnecessary parenthesis

fixes #1111
This commit is contained in:
LJ 2024-02-20 19:22:24 -07:00 committed by GitHub
parent ac05135f59
commit 1577467fe1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 0 deletions

@ -1167,6 +1167,10 @@ export const ns: InternalAPI<NSFull> = {
helpers.log(ctx, () => `Invalid argument: hostname='${hostnameStr}'`); helpers.log(ctx, () => `Invalid argument: hostname='${hostnameStr}'`);
return ""; return "";
} }
if (hostnameStr.startsWith("hacknet-node-") || hostnameStr.startsWith("hacknet-server-")) {
helpers.log(ctx, () => `Invalid argument: hostname='${hostnameStr}' is a reserved hostname.`);
return "";
}
if (Player.purchasedServers.length >= getPurchaseServerLimit()) { if (Player.purchasedServers.length >= getPurchaseServerLimit()) {
helpers.log( helpers.log(

@ -65,6 +65,9 @@ export const renamePurchasedServer = (hostname: string, newName: string): void =
if (!server) throw new Error(`Server '${hostname}' doesn't exists.`); if (!server) throw new Error(`Server '${hostname}' doesn't exists.`);
if (GetServer(newName)) throw new Error(`Server '${newName}' already exists.`); if (GetServer(newName)) throw new Error(`Server '${newName}' already exists.`);
if (!Player.purchasedServers.includes(hostname)) throw new Error(`Server '${hostname}' is not a player server.`); if (!Player.purchasedServers.includes(hostname)) throw new Error(`Server '${hostname}' is not a player server.`);
if (newName.startsWith("hacknet-node-") || newName.startsWith("hacknet-server-")) {
throw new Error(`'${newName}' is a reserved hostname.`);
}
const replace = (arr: string[], old: string, next: string): string[] => { const replace = (arr: string[], old: string, next: string): string[] => {
return arr.map((v) => (v === old ? next : v)); return arr.map((v) => (v === old ? next : v));
}; };
@ -122,6 +125,11 @@ export function purchaseServer(hostname: string, ram: number, cost: number): voi
return; return;
} }
if (hostname.startsWith("hacknet-node-") || hostname.startsWith("hacknet-server-")) {
dialogBoxCreate(`'${hostname}' is a reserved hostname, please try again.`);
return;
}
// Create server // Create server
const newServ = safelyCreateUniqueServer({ const newServ = safelyCreateUniqueServer({
adminRights: true, adminRights: true,