Skip to content

Commit

Permalink
feat(Host): Lock down any operations that only the host can perform. C…
Browse files Browse the repository at this point in the history
…loses #127
  • Loading branch information
alexanderson1993 committed Apr 20, 2022
1 parent f99b9d4 commit 4b162f5
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 17 deletions.
20 changes: 11 additions & 9 deletions client/src/pages/FlightLobby.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ export default function FlightLobby() {
<Menubar>
{clientData.flight && (
<>
<Button
className="btn btn-outline btn-xs btn-error"
onClick={async () => {
await netSend("flightStop");
navigate("/");
}}
>
End
</Button>
{clientData.client.isHost && (
<Button
className="btn btn-outline btn-xs btn-error"
onClick={async () => {
await netSend("flightStop");
navigate("/");
}}
>
End
</Button>
)}
{clientData.flight?.paused ? (
<Button
className="btn btn-outline btn-xs btn-success"
Expand Down
13 changes: 5 additions & 8 deletions server/src/inputs/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ export const clientInputs = {
) => {
let flightClient = context.flightClient;
if (params.clientId) {
// TODO November 18, 2021 - Check to see if the client is the host of the flight.
// This will probably involve checking their Thorium account or associating their
// client ID with the flight somehow.
// For now, we'll just allow anyone to change anyone else's station.
let isHost = true;

if (!isHost || !params.clientId) {
throw new Error("No flight has been started.");
// Only hosts can change other client's station assignment
if (!context.isHost || !params.clientId) {
throw new Error(
"You must be host to change other client's assignments."
);
}
flightClient = context.findFlightClient(params.clientId);
}
Expand Down
5 changes: 5 additions & 0 deletions server/src/inputs/flight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type StationComplementPlugin from "../classes/Plugins/StationComplement";
import {generateIncrementedName} from "../utils/generateIncrementedName";
import {getFlights} from "../utils/getFlights";
import type BasePlugin from "../classes/Plugins";
import inputAuth from "../utils/inputAuth";

const fs = process.env.NODE_ENV === "test" ? {unlink: () => {}} : promises;
interface FlightStartShips {
Expand Down Expand Up @@ -40,6 +41,7 @@ export const flightInputs = {
ships: FlightStartShips[];
}
) => {
inputAuth(context);
if (context.flight) return context.flight;
const flightData = await getFlights();
flightName = generateIncrementedName(
Expand Down Expand Up @@ -126,6 +128,7 @@ export const flightInputs = {
return context.flight;
},
flightLoad(context: DataContext, params: {flightName: string}) {
inputAuth(context);
if (context.flight) return context.flight;

context.flight = new FlightDataModel(
Expand Down Expand Up @@ -162,6 +165,7 @@ export const flightInputs = {
return context.flight;
},
flightStop(context: DataContext): null {
inputAuth(context);
// Save the flight, but don't delete it.
if (!context.flight) return null;
context.flight.paused = false;
Expand All @@ -178,6 +182,7 @@ export const flightInputs = {
context: DataContext,
{flightName}: {flightName: string}
): Promise<null> {
inputAuth(context);
if (context.flight?.name === flightName) {
context.flight = null;
context.server.activeFlightName = null;
Expand Down
5 changes: 5 additions & 0 deletions server/src/inputs/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import fs from "fs/promises";
import path from "path";
import {thoriumPath} from "../../utils/appPaths";
import {getPlugin, publish} from "./utils";
import inputAuth from "server/src/utils/inputAuth";

export const pluginInputs = {
pluginCreate(context: DataContext, params: {name: string}) {
inputAuth(context);
const plugin = new BasePlugin(params, context.server);
context.server.plugins.push(plugin);
publish(plugin.id);
return {pluginId: plugin.id};
},
async pluginDelete(context: DataContext, params: {pluginId: string}) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
await plugin.removeFile();
context.server.plugins.splice(context.server.plugins.indexOf(plugin), 1);
Expand All @@ -22,6 +25,7 @@ export const pluginInputs = {
context: DataContext,
params: {pluginId: string; name: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);

const pluginCopy = plugin.duplicate(params.name);
Expand All @@ -40,6 +44,7 @@ export const pluginInputs = {
active?: boolean;
}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
if (params.description) {
plugin.description = params.description;
Expand Down
2 changes: 2 additions & 0 deletions server/src/inputs/plugins/shipSystems/impulseEngines.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ImpulseEnginesPlugin from "server/src/classes/Plugins/ShipSystems/ImpulseEngines";
import {DataContext} from "server/src/utils/DataContext";
import inputAuth from "server/src/utils/inputAuth";
import {pubsub} from "server/src/utils/pubsub";
import {KilometerPerSecond, KiloNewtons} from "server/src/utils/unitTypes";
import {getPlugin} from "../utils";
Expand Down Expand Up @@ -30,6 +31,7 @@ export const impulseEnginesPluginInput = {
thrust?: KiloNewtons;
}
) {
inputAuth(context);
const shipSystem = getShipSystem(
context,
params.pluginId,
Expand Down
4 changes: 4 additions & 0 deletions server/src/inputs/plugins/shipSystems/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ShipSystemTypes} from "server/src/classes/Plugins/ShipSystems/shipSystemTypes";
import {DataContext} from "server/src/utils/DataContext";
import inputAuth from "server/src/utils/inputAuth";
import {pubsub} from "server/src/utils/pubsub";
import {getPlugin} from "../utils";

Expand All @@ -8,6 +9,7 @@ export const shipSystemsPluginInput = {
context: DataContext,
params: {pluginId: string; name: string; type: keyof typeof ShipSystemTypes}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const ShipSystemClass = ShipSystemTypes[params.type];
const shipSystem = new ShipSystemClass({name: params.name}, plugin);
Expand All @@ -20,6 +22,7 @@ export const shipSystemsPluginInput = {
context: DataContext,
params: {pluginId: string; shipSystemId: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const shipSystem = plugin.aspects.shipSystems.find(
s => s.name === params.shipSystemId
Expand All @@ -45,6 +48,7 @@ export const shipSystemsPluginInput = {
tags?: string[];
}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const shipSystem = plugin.aspects.shipSystems.find(
s => s.name === params.shipSystemId
Expand Down
10 changes: 10 additions & 0 deletions server/src/inputs/plugins/ships/decks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "server/src/classes/Plugins/Ship/Deck";
import type ShipPlugin from "../../../classes/Plugins/Ship";
import {generateIncrementedName} from "server/src/utils/generateIncrementedName";
import inputAuth from "server/src/utils/inputAuth";

function getDeck(
context: DataContext,
Expand Down Expand Up @@ -41,6 +42,7 @@ export const decksPluginInputs = {
context: DataContext,
params: {pluginId: string; shipId: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const ship = plugin.aspects.ships.find(ship => ship.name === params.shipId);
if (!ship) return null;
Expand All @@ -57,6 +59,7 @@ export const decksPluginInputs = {
context: DataContext,
params: {pluginId: string; shipId: string; index: number}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const ship = plugin.aspects.ships.find(ship => ship.name === params.shipId);
if (!ship) return;
Expand All @@ -80,6 +83,7 @@ export const decksPluginInputs = {
| {backgroundImage: File | string | null}
)
) {
inputAuth(context);
const {ship, deck} = getDeck(context, params);

const deckIndex = ship.decks.findIndex(deck => deck.name === params.deckId);
Expand Down Expand Up @@ -134,6 +138,7 @@ export const decksPluginInputs = {
y: number;
}
) {
inputAuth(context);
const {ship, deck} = getDeck(context, params);
const node = new DeckNode({
x: params.x,
Expand All @@ -151,6 +156,7 @@ export const decksPluginInputs = {
context: DataContext,
params: {pluginId: string; shipId: string; deckId: string; nodeId: number}
) {
inputAuth(context);
const {ship, deck} = getDeck(context, params);
const node = deck.nodes.find(node => node.id === params.nodeId);
if (!node) return;
Expand Down Expand Up @@ -185,6 +191,7 @@ export const decksPluginInputs = {
| {flags: NodeFlag[]}
)
) {
inputAuth(context);
const {ship, deck} = getDeck(context, params);
const node = deck.nodes.find(node => node.id === params.nodeId);
if (!node) return;
Expand Down Expand Up @@ -229,6 +236,7 @@ export const decksPluginInputs = {
context: DataContext,
params: {pluginId: string; shipId: string; from: number; to: number}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const ship = plugin.aspects.ships.find(ship => ship.name === params.shipId);
if (!ship) throw new Error("Ship not found");
Expand All @@ -249,6 +257,7 @@ export const decksPluginInputs = {
context: DataContext,
params: {pluginId: string; shipId: string; edgeId: number}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const ship = plugin.aspects.ships.find(ship => ship.name === params.shipId);
if (!ship) throw new Error("Ship not found");
Expand All @@ -269,6 +278,7 @@ export const decksPluginInputs = {
| {flags: EdgeFlag[]}
)
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const ship = plugin.aspects.ships.find(ship => ship.name === params.shipId);
if (!ship) throw new Error("Ship not found");
Expand Down
4 changes: 4 additions & 0 deletions server/src/inputs/plugins/ships/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {DataContext} from "server/src/utils/DataContext";
import {pubsub} from "server/src/utils/pubsub";
import {getPlugin} from "../utils";
import {thoriumPath} from "server/src/utils/appPaths";
import inputAuth from "server/src/utils/inputAuth";

export const shipsPluginInputs = {
pluginShipCreate(
context: DataContext,
params: {pluginId: string; name: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const ship = new ShipPlugin({name: params.name}, plugin);
plugin.aspects.ships.push(ship);
Expand All @@ -22,6 +24,7 @@ export const shipsPluginInputs = {
context: DataContext,
params: {pluginId: string; shipId: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const ship = plugin.aspects.ships.find(ship => ship.name === params.shipId);
if (!ship) return;
Expand Down Expand Up @@ -49,6 +52,7 @@ export const shipsPluginInputs = {
theme?: {themeId: string; pluginId: string};
}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
if (!params.shipId) throw new Error("Ship ID is required");
const ship = plugin.aspects.ships.find(ship => ship.name === params.shipId);
Expand Down
7 changes: 7 additions & 0 deletions server/src/inputs/plugins/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {DataContext} from "server/src/utils/DataContext";
import {pubsub} from "server/src/utils/pubsub";
import {getPlugin} from "./utils";
import defaultCSS from "./defaultTheme";
import inputAuth from "server/src/utils/inputAuth";

export const themesPluginInput = {
async pluginThemeCreate(
context: DataContext,
params: {pluginId: string; name: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const theme = new ThemePlugin({name: params.name}, plugin);
plugin.aspects.themes.push(theme);
Expand All @@ -22,6 +24,7 @@ export const themesPluginInput = {
context: DataContext,
params: {pluginId: string; themeId: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const theme = plugin.aspects.themes.find(
theme => theme.name === params.themeId
Expand All @@ -36,6 +39,7 @@ export const themesPluginInput = {
context: DataContext,
params: {pluginId: string; themeId: string; name?: string; rawCSS?: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const theme = plugin.aspects.themes.find(
theme => theme.name === params.themeId
Expand All @@ -59,6 +63,7 @@ export const themesPluginInput = {
context: DataContext,
params: {pluginId: string; themeId: string; name: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const theme = plugin.aspects.themes.find(
theme => theme.name === params.themeId
Expand All @@ -77,6 +82,7 @@ export const themesPluginInput = {
fileName: string;
}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const theme = plugin.aspects.themes.find(
theme => theme.name === params.themeId
Expand All @@ -96,6 +102,7 @@ export const themesPluginInput = {
context: DataContext,
params: {pluginId: string; themeId: string; file: string}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const theme = plugin.aspects.themes.find(
theme => theme.name === params.themeId
Expand Down
4 changes: 4 additions & 0 deletions server/src/inputs/plugins/universe/planets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import path from "path";
import {promises as fs} from "fs";
import {thoriumPath} from "server/src/utils/appPaths";
import {SatelliteComponent} from "server/src/components/satellite";
import inputAuth from "server/src/utils/inputAuth";

function getSystem(context: DataContext, pluginId: string, systemId: string) {
const plugin = getPlugin(context, pluginId);
Expand Down Expand Up @@ -96,6 +97,7 @@ export const planetPluginInputs = {
planetType: PlanetTypes;
}
) {
inputAuth(context);
const system = getSystem(context, params.pluginId, params.solarSystemId);
const childrenPlanets = system.planets;

Expand Down Expand Up @@ -167,6 +169,7 @@ export const planetPluginInputs = {
planetId: string;
}
) {
inputAuth(context);
const system = getSystem(context, params.pluginId, params.solarSystemId);
const planet = system.planets.find(s => s.name === params.planetId);
if (!system) {
Expand Down Expand Up @@ -205,6 +208,7 @@ export const planetPluginInputs = {
satellite?: Partial<SatelliteComponent>;
}
) {
inputAuth(context);
const system = getSystem(context, params.pluginId, params.solarSystemId);
const planet = system.planets.find(s => s.name === params.planetId);
if (!planet) {
Expand Down
4 changes: 4 additions & 0 deletions server/src/inputs/plugins/universe/solarSystems.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SolarSystemPlugin from "server/src/classes/Plugins/Universe/SolarSystem";
import {DataContext} from "server/src/utils/DataContext";
import {generateIncrementedName} from "server/src/utils/generateIncrementedName";
import inputAuth from "server/src/utils/inputAuth";
import {pubsub} from "server/src/utils/pubsub";
import {AstronomicalUnit, LightMinute} from "server/src/utils/unitTypes";
import {getPlugin} from "../utils";
Expand All @@ -17,6 +18,7 @@ export const solarSystemsPluginInputs = {
};
}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const solarSystem = new SolarSystemPlugin(
{position: params.position},
Expand All @@ -35,6 +37,7 @@ export const solarSystemsPluginInputs = {
solarSystemId: string;
}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
const solarSystem = plugin.aspects.solarSystems.find(
solarSystem => solarSystem.name === params.solarSystemId
Expand Down Expand Up @@ -66,6 +69,7 @@ export const solarSystemsPluginInputs = {
skyboxKey?: string;
}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
if (!params.solarSystemId) throw new Error("Solar System ID is required");
const solarSystem = plugin.aspects.solarSystems.find(
Expand Down
Loading

0 comments on commit 4b162f5

Please sign in to comment.