TERMINAL: Stop terminal scp from revealing and copying to unreachable servers (#1542)

This commit is contained in:
Yichi Zhang 2024-08-03 23:38:00 -07:00 committed by GitHub
parent d9394db543
commit 0dd42a1d57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 21 deletions

@ -22,35 +22,19 @@ import "../Script/RunningScript"; // For reviver side-effect
let AllServers: Record<string, Server | HacknetServer> = {};
function GetServerByIP(ip: string): BaseServer | undefined {
for (const key of Object.keys(AllServers)) {
const server = AllServers[key];
for (const server of Object.values(AllServers)) {
if (server.ip !== ip) continue;
return server;
}
}
//Returns server object with corresponding hostname
// Relatively slow, would rather not use this a lot
function GetServerByHostname(hostname: string): BaseServer | null {
for (const key of Object.keys(AllServers)) {
const server = AllServers[key];
if (server.hostname == hostname) {
return server;
}
}
return null;
}
//Get server by IP or hostname. Returns null if invalid
export function GetServer(s: string): BaseServer | null {
if (Object.hasOwn(AllServers, s)) {
const server = AllServers[s];
if (server) return server;
}
if (!isIPAddress(s)) return GetServerByHostname(s);
if (!isIPAddress(s)) return null;
const ipserver = GetServerByIP(s);
if (ipserver !== undefined) {
return ipserver;
@ -59,6 +43,14 @@ export function GetServer(s: string): BaseServer | null {
return null;
}
//Get server by IP or hostname. Returns null if invalid or unreachable.
export function GetReachableServer(s: string): BaseServer | null {
const server = GetServer(s);
if (server === null) return server;
if (server.serversOnNetwork.length === 0) return null;
return server;
}
export function GetAllServers(): BaseServer[] {
const servers: BaseServer[] = [];
for (const key of Object.keys(AllServers)) {

@ -1,6 +1,6 @@
import { Terminal } from "../../Terminal";
import { BaseServer } from "../../Server/BaseServer";
import { GetServer } from "../../Server/AllServers";
import { GetReachableServer } from "../../Server/AllServers";
import { hasScriptExtension } from "../../Paths/ScriptFilePath";
import { hasTextExtension } from "../../Paths/TextFilePath";
import { isMember } from "../../utils/EnumHelper";
@ -14,7 +14,7 @@ export function scp(args: (string | number | boolean)[], server: BaseServer): vo
// Validate destination server
const destHostname = String(args.pop());
const destServer = GetServer(destHostname);
const destServer = GetReachableServer(destHostname);
if (!destServer) return Terminal.error(`Invalid destination server: ${destHostname}`);
// Validate filepaths

@ -125,7 +125,12 @@ export async function getTabCompletionPossibilities(terminalText: string, baseDi
const addGlobalAliases = () => addGeneric({ iterable: GlobalAliases.keys() });
const addCommands = () => addGeneric({ iterable: gameCommands });
const addDarkwebItems = () => addGeneric({ iterable: Object.values(DarkWebItems).map((item) => item.program) });
const addServerNames = () => addGeneric({ iterable: GetAllServers().map((server) => server.hostname) });
const addServerNames = () =>
addGeneric({
iterable: GetAllServers()
.filter((server) => server.serversOnNetwork.length !== 0)
.map((server) => server.hostname),
});
const addScripts = () => addGeneric({ iterable: currServ.scripts.keys(), usePathing: true });
const addTextFiles = () => addGeneric({ iterable: currServ.textFiles.keys(), usePathing: true });
const addCodingContracts = () => {