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

@@ -7,17 +7,23 @@ import Command from "./server/commands/command.ts";
import { ServerContentBase } from "./server/ContentBase.ts";
import z from "zod";
import { verify } from "@hono/hono/jwt";
import { type ProfileToken } from "./server/profiles/types/profile.ts";
import { SignalRSocketHandler } from "./server/socket/signalr/socket.ts";
import { PushNotificationId } from "./server/socket/signalr/types.ts";
import { genericResponse } from "./util/api.ts";
import { getNetConfig } from "./net.ts";
import { TokenFormat, TokenType } from "./server/platforms/types.ts";
LoggingConfiguration.resetTimeFormat = TimeFormat.Unix;
LoggingConfiguration.resetLogTiming = LogTiming.Microtask;
const log = new Logging("Main");
log.i(`wsi by zombieb`);
log.i(`Galvanic Corrosion rewritten`);
if (Deno.args.includes('--flush-persistence')) {
await Deno.remove('./persist', { recursive: true });
log.w(`Persistence was wiped!`);
}
export function detailedLog(items: (string | number | boolean | null)[]) {
return items.filter(val => val !== null).join('\r\n ');
@@ -56,11 +62,14 @@ const onListen = async () => {
Object.values(Server).forEach(base => ((base as ServerContentBase).start ? (base as ServerContentBase).start() : undefined));
}
const server = Deno.serve({ hostname: "10.0.1.39", port: 13370, onListen: addr => {
log.n(`Listening info: ${JSON.stringify(addr)}`);
const netConfig = getNetConfig();
const server = Deno.serve({
hostname: netConfig.host, port: netConfig.port, onListen: addr => {
log.n(`Listening info: ${JSON.stringify(addr)}`);
onListen();
}}, async (req, info) => {
onListen();
}
}, async (req, info) => {
const url = new URL(req.url);
const srcAddr = getSourceAddress(req, info.remoteAddr);
@@ -68,9 +77,9 @@ const server = Deno.serve({ hostname: "10.0.1.39", port: 13370, onListen: addr =
if (url.pathname == '/notify/hub/v1/negotiate') {
return new Response(JSON.stringify({
connectionId: "who_said_it",
availableTransports: [{transport:"WebSockets",transferFormats:["Text"]}]
}), { headers: { 'Content-Type': 'application/json' }});
connectionId: "galv4",
availableTransports: [{ transport: "WebSockets", transferFormats: ["Text"] }]
}), { headers: { 'Content-Type': 'application/json' } });
}
if (req.headers.get('Connection')?.includes('Upgrade') && req.headers.get('Upgrade')?.includes('websocket')) {
const isSignalR = url.searchParams.has('id');
@@ -87,7 +96,11 @@ const server = Deno.serve({ hostname: "10.0.1.39", port: 13370, onListen: addr =
log.w(`No secret set!`);
return unauthRes;
}
const payload = (await verify(splitHeader, secret)) as ProfileToken;
const payload = JSON.parse(JSON.stringify(await verify(splitHeader, secret))) as TokenFormat;
if (payload.typ !== TokenType.Access) {
log.w(`Only access tokens can be used to connect to the socket`);
return unauthRes;
}
const profile = await Server.Profiles.get(payload.sub);
if (!profile) return new Response("Internal Server Error (profile)", { status: 500 });
@@ -118,11 +131,11 @@ const server = Deno.serve({ hostname: "10.0.1.39", port: 13370, onListen: addr =
}
}
const res = await AppRoot.app.fetch(req, { srcAddr });
const netlog = detailedLog([srcAddr,
`${res.status}: ${req.method} ${getFullPathFromUrl(new URL(req.url))}`,
`${typeof res.status == 'number' ? res.status : "SENT STACK TRACE"}: ${req.method} ${getFullPathFromUrl(new URL(req.url))}`,
formatHeader(req.headers, 'Content-Type'),
formatHeader(req.headers, 'Connection'),
formatHeader(req.headers, 'User-Agent'),
@@ -148,6 +161,8 @@ Deno.addSignalListener('SIGINT', () => {
for (const socket of consoleSockets) socket.destroy();
for (const socket of gameSockets) socket.sendNotification(PushNotificationId.ModerationQuitGame);
Object.values(Server).forEach(base => ((base as ServerContentBase).destroy ? (base as ServerContentBase).destroy() : undefined));
});
Server.Commands.addRootCommand(new Command({