temp: delete all server routes

This commit is contained in:
not-nullptr 2024-03-13 00:17:26 +00:00
parent 42e345b1e8
commit 0777a72216
10 changed files with 0 additions and 481 deletions

View file

@ -1,13 +0,0 @@
import { RoomManager } from "$lib/server/class/Room.js";
import { useAuth } from "$lib/util/api";
export async function load(opts) {
const apiKey = opts.cookies.get("token");
const user = await useAuth(apiKey || "unused");
const rooms = RoomManager.getRooms().map((r) => r.toJSON());
return {
user: { ...user },
rooms,
token: apiKey,
};
}

View file

@ -1,80 +0,0 @@
<!-- <script lang="ts">
import { browser } from "$app/environment";
import { goto } from "$app/navigation";
import { getContext } from "svelte";
import type { PageData } from "./$types";
import type { Writable } from "svelte/store";
const token = getContext<Writable<string>>("token");
let copyText = "Copy token";
export let data: PageData;
$: b64Token = btoa(data.token || "");
$: {
if (Object.keys(data.user).length === 0 && browser) {
$token = "";
document.cookie =
"token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC; samesite=strict";
console.log("no user");
goto("/signup");
}
}
function copyToken() {
navigator.clipboard.writeText(b64Token);
copyText = "Copied!";
setTimeout(() => {
copyText = "Copy token";
}, 2000);
}
</script>
<div class="relative h-[calc(100vh-200px)] flex-col gap-6 overflow-hidden">
<div
class="relative flex w-full flex-col gap-6 overflow-hidden rounded-[2.25rem] bg-black p-8 md:p-12"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="512"
height="525"
viewBox="0 0 512 525"
fill="none"
style="animation-duration: 300s; transform-origin: 50% 50%; animation-iteration-count: infinite; animation-timing-function: linear; animation-name: spin; animation-delay: 0s; animation-direction: normal; animation-fill-mode: none; animation-play-state: running;"
class="pointer-events-none absolute -bottom-[18rem] right-0 z-0 animate-spin opacity-20"
>
<path
d="M511.5 262.12C511.5 353.613 465.547 434.182 396.019 480.947C408.179 457.937 415.083 431.597 415.083 403.617C415.083 313.723 343.816 240.744 255.992 240.744C191.257 240.744 138.692 186.941 138.692 120.622C138.692 54.3027 191.257 0.5 255.992 0.5C397.026 0.5 511.5 117.695 511.5 262.12ZM255.992 53.5225C243.745 53.5225 233.816 63.7047 233.816 76.2224C233.816 88.7388 243.745 98.9223 255.992 98.9223C268.257 98.9223 278.173 88.7387 278.173 76.2224C278.173 63.7048 268.257 53.5225 255.992 53.5225ZM299.355 97.9223C287.104 97.9223 277.173 108.104 277.173 120.622C277.173 133.139 287.104 143.322 299.355 143.322C311.62 143.322 321.536 133.139 321.536 120.622C321.536 108.104 311.62 97.9223 299.355 97.9223ZM212.635 97.9223C200.382 97.9223 190.455 108.104 190.455 120.622C190.455 133.139 200.382 143.322 212.635 143.322C224.889 143.322 234.816 133.139 234.816 120.622C234.816 108.104 224.888 97.9223 212.635 97.9223ZM255.992 142.322C243.745 142.322 233.816 152.505 233.816 165.021C233.816 177.539 243.745 187.721 255.992 187.721C268.257 187.721 278.173 177.538 278.173 165.021C278.173 152.505 268.257 142.322 255.992 142.322Z"
stroke="white"
/>
<path
d="M0.5 262.119C0.5 170.626 46.444 90.0553 115.976 43.2909C103.82 66.3019 96.9172 92.6424 96.9172 120.622C96.9172 210.516 168.174 283.495 255.992 283.495C320.735 283.495 373.305 337.298 373.305 403.617C373.305 469.934 320.735 523.739 255.992 523.739C114.974 523.739 0.5 406.544 0.5 262.119ZM255.992 336.517C243.744 336.517 233.816 346.7 233.816 359.217C233.816 371.735 243.745 381.917 255.992 381.917C268.256 381.917 278.173 371.735 278.173 359.217C278.173 346.701 268.256 336.517 255.992 336.517ZM299.355 380.917C287.104 380.917 277.173 391.099 277.173 403.617C277.173 416.135 287.104 426.317 299.355 426.317C311.619 426.317 321.536 416.135 321.536 403.617C321.536 391.099 311.619 380.917 299.355 380.917ZM255.992 425.317C243.745 425.317 233.816 435.499 233.816 448.016C233.816 460.533 243.744 470.717 255.992 470.717C268.256 470.717 278.173 460.533 278.173 448.016C278.173 435.499 268.256 425.317 255.992 425.317ZM212.634 380.917C200.382 380.917 190.454 391.099 190.454 403.617C190.454 416.135 200.382 426.317 212.634 426.317C224.888 426.317 234.816 416.135 234.816 403.617C234.816 391.099 224.888 380.917 212.634 380.917Z"
stroke="white"
/>
</svg>
<h1 class="text-[36px] leading-[1.41] md:text-[60px] md:leading-[1.1]">
suyu Online Services
</h1>
<p class="text-wrap text-lg leading-relaxed text-[#A6A5A7]">
Your token should be kept private. If you believe it has been compromised, please
contact us immediately.
</p>
<div class="flex gap-4">
<div
class="input !w-fit max-w-full select-all overflow-hidden text-ellipsis whitespace-pre"
>
<p
style="transition: 180ms ease; transition-property: filter;"
class="w-fit blur hover:blur-none"
>
{b64Token}
</p>
</div>
<button class="button-sm" on:click={copyToken}>{copyText}</button>
</div>
<div class="flex gap-4">
<a href="/account/friends" class="button-sm">Manage Friends</a>
<a href="/account/rooms" class="button-sm">Rooms</a>
</div>
</div>
</div> -->

View file

@ -1,122 +0,0 @@
// TODO: refactor into external utils (ie Suyu.createAccount() or something???)
import { userRepo } from "$lib/server/repo";
import type { SuyuUser } from "$lib/server/schema";
import { json, serializeRoles } from "$lib/server/util";
import { useAuth } from "$lib/util/api";
import type {
CreateAccountRequest,
CreateAccountResponse,
DeleteAccountResponse,
GetUserResponse,
} from "$types/api";
import crypto from "crypto";
import { promisify } from "util";
import { verify } from "hcaptcha";
import { PUBLIC_SITE_KEY } from "$env/static/public";
import { HCAPTCHA_KEY } from "$env/static/private";
import validator from "validator";
const randomBytes = promisify(crypto.randomBytes);
async function genKey(username: string) {
const random = (await randomBytes(80)).toString("hex");
let apiKey = `${username}:${random}`;
let b64ApiKey = Buffer.from(apiKey).toString("base64");
if (b64ApiKey.length > 80) {
b64ApiKey = b64ApiKey.slice(0, 80);
}
// decode b64ApiKey
apiKey = Buffer.from(b64ApiKey, "base64").toString("utf-8");
return apiKey;
}
export async function POST({ request, getClientAddress }) {
const body: CreateAccountRequest = await request.json();
if (!body.username || !body.email || !body.captchaToken) {
return json<CreateAccountResponse>({
success: false,
error: "missing fields",
});
}
if (body.username.length < 3 || body.username.length > 24) {
return json<CreateAccountResponse>({
success: false,
error: "invalid username",
});
}
if (!validator.isEmail(body.email)) {
return json<CreateAccountResponse>({
success: false,
error: "invalid email",
});
}
const res = await verify(HCAPTCHA_KEY, body.captchaToken, getClientAddress(), PUBLIC_SITE_KEY);
if (!res.success) {
return json<CreateAccountResponse>({
success: false,
error: "missing fields!",
});
}
// check if user exists
const user = await userRepo.findOne({
where: [
{
username: body.username,
},
{
email: body.email,
},
],
});
if (user) {
return json<CreateAccountResponse>({
success: false,
error: "user already exists",
});
}
// the api key can only be 80 characters total, including the username and colon
const key = await genKey(body.username);
const createdUser: SuyuUser = userRepo.create({
username: body.username,
avatarUrl: `https://avatars.githubusercontent.com/u/${Math.floor(Math.random() * 100000000)}?v=4`,
displayName: body.username,
roles: serializeRoles(["user"]),
apiKey: key,
email: body.email,
});
await userRepo.save(createdUser);
return json<CreateAccountResponse>({
success: true,
token: createdUser.apiKey,
user: createdUser,
});
}
export async function GET({ request }) {
const user = await useAuth(request);
if (!user) {
return json<GetUserResponse>({
success: false,
error: "unauthorized",
});
}
return json<GetUserResponse>({
success: true,
user,
});
}
export async function DELETE({ request }) {
const user = await useAuth(request);
if (!user) {
return json<DeleteAccountResponse>({
success: false,
error: "unauthorized",
});
}
await userRepo.remove(user);
return json<DeleteAccountResponse>({
success: true,
});
}

View file

@ -1,47 +0,0 @@
import { json } from "$lib/server/util/index.js";
export function GET({ request }) {
return new Response(
`-----BEGIN CERTIFICATE-----
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
-----END CERTIFICATE-----`,
{
headers: {
"content-type": "text/plain",
},
},
);
}
export function POST({ request }) {
return new Response();
}

View file

@ -1,16 +0,0 @@
import { PRIVATE_KEY } from "$lib/server/secrets/secrets.json";
import { useAuth } from "$lib/util/api/index.js";
import jwt from "jsonwebtoken";
export async function POST({ request }) {
const userKey = `${request.headers.get("x-username")}:${request.headers.get("x-token")}`;
const user = await useAuth(userKey);
const token = jwt.sign({ ...user, apiKey: userKey }, Buffer.from(PRIVATE_KEY), {
algorithm: "RS256",
});
return new Response(token, {
headers: {
"content-type": "text/html",
},
});
}

View file

@ -1,65 +0,0 @@
import { Room, RoomManager } from "$lib/server/class/Room";
import { userRepo } from "$lib/server/repo/index.js";
import { SuyuUser } from "$lib/server/schema";
import { PUBLIC_KEY } from "$lib/server/secrets/secrets.json";
import { json } from "$lib/server/util";
import { useAuth } from "$lib/util/api/index.js";
import type { IJwtData } from "$types/auth.js";
import type { IRoom, LobbyResponse } from "$types/rooms";
import jwt from "jsonwebtoken";
export async function GET({ request }) {
return json<LobbyResponse>({
rooms: RoomManager.getRooms().map((r) => r.toJSON()),
});
}
/* credit to janeberru for showing the shape of this data */
export async function POST({ request, getClientAddress }) {
// TODO: per-ip room limit
const body: IRoom = await request.json();
/* description may contain "### END DESCRIPTION ###" on its own line. if it does, get all lines after that */
const parsedDescription = body.description.split("### END DESCRIPTION ###");
console.log(parsedDescription);
const description = parsedDescription?.slice(1)?.join("### END DESCRIPTION ###") || "";
const opts: { [key: string]: string } = {};
description.split("\n").forEach((line) => {
const [key, ...values] = line.split("=");
const value = values.join("=").trim();
if (!key || !value) return;
opts[key] = value;
});
if (opts.ip) {
if (
!opts.ip.match(
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
)
) {
return new Response(null, { status: 400 });
}
}
const token = request.headers.get("authorization");
if (!token) return new Response(null, { status: 401 });
// TODO: jwt utils which type and validate automatically
const user = await useAuth(token);
console.log(user);
if (!user) return new Response(null, { status: 401 });
const room = RoomManager.createRoom({
name: body.name,
description: parsedDescription[0] || "",
gameName: body.preferredGameName,
gameId: body.preferredGameId,
players: [
{
gameId: 0,
gameName: "",
nickname: user.username,
},
],
maxPlayers: body.maxPlayers,
ip: `${opts.ip || getClientAddress().split(":").at(-1)}:${body.port}`,
host: user,
hasPassword: body.hasPassword || false,
});
return json(room.toJSON());
}

View file

@ -1,30 +0,0 @@
import { RoomManager } from "$lib/server/class/Room";
import { json } from "$lib/server/util/index.js";
import { useAuth } from "$lib/util/api/index.js";
/* thanks again janeberru for the shape of this data */
export async function POST({ request, params }) {
const body = await request.json();
const { id } = params;
const room = RoomManager.getRoom(id);
if (!room) return new Response(null, { status: 500 });
const user = await useAuth(request.headers.get("authorization") || "");
if (!user) return new Response(null, { status: 401 });
if (user.id !== room.host.id) return new Response(null, { status: 401 });
if (body.players.length === 0 && room.roomInfo.owner) {
console.log(room.roomInfo.players);
room.setPlayerList([{ gameId: 0, gameName: "", nickname: room.roomInfo.owner }]);
}
return json({ message: "Lobby updated successfully" });
}
export async function DELETE({ request, params }) {
const { id } = params;
const room = RoomManager.getRoom(id);
if (!room) return new Response(null, { status: 500 });
const user = await useAuth(request.headers.get("authorization") || "");
if (!user) return new Response(null, { status: 401 });
if (user.id !== room.host.id) return new Response(null, { status: 401 });
room.delete();
return json(room.toJSON());
}

View file

@ -1,11 +0,0 @@
import { json } from "$lib/server/util/index";
import { useAuth } from "$lib/util/api/index.js";
export async function GET({ request }) {
const user = await useAuth(request.headers.get("authorization") || "");
console.log(user);
if (!user) return new Response(null, { status: 401 });
return json({
username: user.username,
});
}

View file

@ -1,13 +0,0 @@
import { RoomManager } from "$lib/server/class/Room.js";
import { useAuth } from "$lib/util/api";
export async function load(opts) {
const apiKey = opts.cookies.get("token");
const user = await useAuth(apiKey || "unused");
const rooms = RoomManager.getRooms().map((r) => r.toJSON());
return {
user: { ...user },
rooms,
token: apiKey,
};
}

View file

@ -1,84 +0,0 @@
<!-- <script lang="ts">
import { browser } from "$app/environment";
import { goto } from "$app/navigation";
import HCaptcha from "$components/HCaptcha.svelte";
import { PUBLIC_SITE_KEY } from "$env/static/public";
import { SuyuAPI } from "$lib/client/api";
import type { PageData } from "./$types";
import type { Writable } from "svelte/store";
import { getContext } from "svelte";
const token = getContext<Writable<string>>("token");
if ($token) goto("/account");
let usernameInput = "";
let emailInput = "";
let captchaToken = "";
$: disabled = !usernameInput || !emailInput || !captchaToken;
export let data: PageData;
if (Object.keys(data.user).length !== 0 && browser) goto("/account");
async function signUp() {
const res = await SuyuAPI.users.createAccount({
username: usernameInput,
email: emailInput,
captchaToken,
});
if (!res.success) {
// TODO: modal
alert(res.error);
return;
}
// set "token" cookie
document.cookie = `token=${res.token}; path=/; max-age=31536000; samesite=strict`;
$token = res.token;
goto("/account");
}
async function captchaComplete(event: CustomEvent<any>) {
captchaToken = event.detail.token;
}
</script>
<div
class="align-center relative flex h-[calc(100vh-200px)] flex-col items-center justify-center gap-6 overflow-hidden"
>
<div class="flex h-fit w-full max-w-[500px] flex-col rounded-[2.25rem] bg-black p-10">
<h1 class="text-[60px] md:leading-[1.1]">Sign up</h1>
<div class="mt-4 flex flex-col gap-4">
<p>
suyu believes in user privacy; as such, usernames are distributed on a first-come,
first-serve basis, with no password required. Accounts are used for:
</p>
<ul class="[&>*]:before:mr-3 [&>*]:before:content-['•']">
<li>Creating rooms</li>
<li>Adding friends</li>
</ul>
<p>
Lost your account? <a class="link" href="https://discord.gg/suyu" target="_blank"
>Contact us</a
>.
</p>
<input
bind:value={emailInput}
maxlength="128"
class="input"
type="text"
placeholder="Recovery Email"
/>
<input
bind:value={usernameInput}
maxlength="24"
class="input"
type="text"
placeholder="Username"
/>
<div class="h-[78px]">
<HCaptcha on:success={captchaComplete} theme="dark" sitekey={PUBLIC_SITE_KEY} />
</div>
<button {disabled} on:click={signUp} class="cta-button mt-2">Sign up</button>
</div>
</div>
</div> -->