galvanic corrosion rewrite

commit this before something goes horribly wrong
This commit is contained in:
2025-08-12 21:04:52 -04:00
parent 941c8400c0
commit f19552929e
40 changed files with 28149 additions and 73212 deletions

View File

@@ -3,56 +3,13 @@ import Command from "../commands/command.ts";
import { ServerContentBase } from "../ContentBase.ts";
import { transformStringToEnum } from "../../util/validators.ts";
import { sign } from "@hono/hono/jwt";
export enum PlatformType {
All = -1,
Steam,
Oculus,
PlayStation,
Xbox,
WindowsPlatformless,
IOS,
GooglePlay
}
export enum DeviceClass {
Unknown,
VR,
Screen,
Mobile,
VRLow,
Quest2
}
interface DbCachedLogin {
accountId: number,
lastLoginTime: Date,
requirePassword: boolean
}
export interface CachedLogin extends DbCachedLogin {
platformId: string
platform: PlatformType
}
import { CachedLogin, DbCachedLogin, PlatformMask, PlatformType, TokenFormat, TokenType } from "./types.ts";
export const steamAuthTicketSchema = z.object({
Ticket: z.string().min(256),
AppId: z.literal("471710")
});
export enum ProfileRole {
Developer = "developer",
Moderator = "moderator",
Screenshare = "screenshare",
User = "user"
}
interface TokenFormat {
iss: string,
exp: number,
sub: number,
role: string
}
export class PlatformsManager extends ServerContentBase {
static platformsKey = "platforms";
@@ -61,15 +18,15 @@ export class PlatformsManager extends ServerContentBase {
return [PlatformsManager.platformsKey, ...keys.filter(val => typeof val == 'string')];
}
async getToken(accountId: number, role: string) {
async getToken(accountId: number, type: TokenType) {
const secret = Deno.env.get('SECRET');
if (!secret) throw new Error("No SECRET in env. Did you forget to set it?");
const token: TokenFormat = {
typ: type,
sub: accountId,
role,
iss: "https://wsi.proxnet.dev/auth/",
exp: Math.round(Date.now() / 1000) + 21_600
iss: "https://yarns.proxnet.dev/auth/",
exp: type == TokenType.Access ? Math.round(Date.now() / 1000) + 21_600 : Math.round(Date.now() / 1000) + 31_556_952
}
return await sign(JSON.parse(JSON.stringify(token)), secret);
}
@@ -106,8 +63,8 @@ export class PlatformsManager extends ServerContentBase {
};
}
async getCachedLogins(platform: PlatformType, platformId: string, format: true): Promise<CachedLogin[] | null>
async getCachedLogins(platform: PlatformType, platformId: string, format: false): Promise<DbCachedLogin[] | null>
async getCachedLogins(platform: PlatformType, platformId: string, format: true): Promise<CachedLogin[]>
async getCachedLogins(platform: PlatformType, platformId: string, format: false): Promise<DbCachedLogin[]>
async getCachedLogins(platform: PlatformType, platformId: string, format?: boolean) {
const set = await this.kv.getKv().get<Set<DbCachedLogin>>(this.#constructPlatformKey(platform, platformId));
if (set.value && format) return set.value.values().toArray().map(val => ({
@@ -118,7 +75,7 @@ export class PlatformsManager extends ServerContentBase {
requirePassword: val.requirePassword
} as CachedLogin));
else if (set.value) return set.value.values().toArray();
else return null;
else return [];
}
async deleteCachedLogin(platform: PlatformType, platformId: string, accountId: number) {
@@ -136,6 +93,30 @@ export class PlatformsManager extends ServerContentBase {
} else return null;
}
getPlatformMask(value: number) {
const err = new Error("Invalid mask");
if (typeof value !== 'number' || !Number.isInteger(value)) throw err;
if (value === PlatformMask.All) {
return [PlatformMask.All];
}
return Object.values(PlatformMask)
.filter(v => typeof v === "number" && v !== PlatformMask.None && v !== PlatformMask.All)
.filter(v => (value & (v as number)) === v) as PlatformMask[];
}
buildPlatformMask(...flags: PlatformMask[]) {
const err = new Error("Invalid mask");
if (!flags.length) throw err;
for (const flag of flags)
if (typeof flag !== 'number' || !Object.values(PlatformMask).includes(flag))
throw err;
return flags.reduce((mask, flag) => mask | flag, 0);
}
override start() {
this.server.Commands.addRootCommand(new Command({
key: ['platforms', 'pm', 'platformmanager', 'platformanager'],