duhhhhhhhh

This commit is contained in:
2025-09-11 13:47:30 -04:00
parent eef3667618
commit 317da3aaf7
53 changed files with 1395 additions and 212 deletions

View File

@@ -134,4 +134,42 @@ export class RateLimiter {
#close() {
clearInterval(this.#intervalId);
}
}
const loginLockBodySchema = z.object({
LoginLock: z.uuidv4()
});
export const loginLockMiddleware = async (c: Context<HonoEnv>, nxt: Next) => {
function unauthorized() {
return statusResponse(c, HTTPStatus.Unauthorized);
}
if (c.req.header("Content-Type") !== "application/x-www-form-urlencoded") return unauthorized();
try {
const form = await c.req.formData();
const body = await loginLockBodySchema.safeParseAsync(Object.fromEntries(form.entries()));
if (body.success) {
if (typeof c.get('profile') == 'undefined') {
log.w(`Profile was not set, cannot validate LoginLock. Was the request authorized?`);
return statusResponse(c, HTTPStatus.InternalServerError);
}
const profile = c.get('profile');
const loginLock = await profile.Matchmaking.getLoginLock();
if (!loginLock) await profile.Matchmaking.setLoginLock(body.data.LoginLock);
else if (body.data.LoginLock !== loginLock) {
log.w(`LoginLock did not match. The token for this profile could be compromised or the client is an unknown state.`);
return unauthorized();
}
return await nxt();
} else {
log.w(`LoginLock parse failed: ${JSON.stringify(body.error)}`);
return unauthorized();
}
} catch {
return unauthorized();
}
}

View File

@@ -11,23 +11,23 @@ export async function routeImporter(hono: Hono<HonoEnv>, prefix: string, paths:
for (const route of items) hono.route(route.path, route.app);
}
export async function importer<T>(importKey: string, prefix: string, paths: string[]): Promise<T[]> {
const log = new Logging(`Importer:'${importKey}'-${prefix}`);
export async function importer<T>(importKey: string, p: string, paths: string[]): Promise<T[]> {
const log = new Logging(`Importer:'${importKey}':${p}`);
const items: T[] = [];
for (const pathStr of paths) {
const importPath = path.join(process.cwd(), prefix, pathStr);
const importPath = path.join(process.cwd(), p, pathStr);
if (debug) log.d(`'${importKey}' found ${importPath}`);
for await (const localPath of Deno.readDir(importPath)) {
if (localPath.isDirectory) continue;
if (localPath.isFile && localPath.name.endsWith('.ts')) {
const fullPath = path.join('file://', importPath, localPath.name);
const fullPath = path.join(importPath, localPath.name);
if (debug) log.d(`'${importKey}' importing ${fullPath}`);
await import(fullPath).then(val => {
await import(`file://${fullPath}`).then(val => {
if (val[importKey]) items.push(val[importKey]);
else log.w(`Import key '${importKey}' not found on: '${fullPath}'`);

View File

@@ -43,9 +43,25 @@ export const transformStringToEnum = <T>(anEnum: { [s: string]: string | number
export const transformCheckEnum = <T>(anEnum: { [s: string]: string | number }) => {
return (arg: number | string, ctx: z.RefinementCtx<number | string>) => {
if (typeof anEnum[arg] == 'undefined') {
ctx.addIssue("Not an enum member");
function invalid() {
ctx.addIssue("Invalid enum member");
}
if (typeof arg == 'string') {
if (typeof anEnum[arg] == 'number') return anEnum[arg] as T;
else {
invalid();
return null;
}
} else if (typeof arg == 'number') {
if (typeof anEnum[arg] == 'string') return arg as T;
else {
invalid();
return null;
}
} else {
invalid();
return null;
} else return anEnum[arg] as T;
}
}
}