testing stuffs

This commit is contained in:
cirroskais 2024-03-18 22:39:00 -04:00
parent 451a02d2e8
commit f5be5beb42
No known key found for this signature in database
GPG key ID: 5FC73EBF2678E33D
5 changed files with 73 additions and 11 deletions

1
src/env.d.ts vendored
View file

@ -2,6 +2,7 @@ declare module "bun" {
interface Env { interface Env {
DISCORD_CLIENT_ID: string; DISCORD_CLIENT_ID: string;
DISCORD_CLIENT_SECRET: string; DISCORD_CLIENT_SECRET: string;
DISCORD_CLIENT_TOKEN: string;
ENCRYPTION_KEY: string; ENCRYPTION_KEY: string;
ENCRYPTION_SALT: string; ENCRYPTION_SALT: string;
} }

View file

@ -1,23 +1,51 @@
import token from "./routes/token"; import token from "./routes/token";
import config from "./routes/config"; import config from "./routes/config";
import user from "./routes/user";
import negotiate from "./routes/negotiate"; import negotiate from "./routes/negotiate";
Bun.serve({ type WebSocketData = {
authenticated: boolean;
identity: {
id: string;
username: string;
avatar: string;
};
};
const terryIdentity = {
id: "869016244613951539",
username: "terry218742",
avatar: "",
};
const server = Bun.serve<WebSocketData>({
port: 3000, port: 3000,
async fetch(req) { async fetch(req, server) {
const url = new URL(req.url); const url = new URL(req.url);
console.log(`${new Date().toUTCString()} ${req.method} ${url.pathname}`); console.log(`${new Date().toUTCString()} ${req.method} ${url.pathname}`);
if (url.pathname === "/") return await config(req); if (url.pathname === "/") return await config(req);
if (url.pathname === "/ws") return await negotiate(req, server);
if (url.pathname === "/token") return await token(req); if (url.pathname === "/token") return await token(req);
if (url.pathname === "/negotiate") return await negotiate(req); if (url.pathname === "/user") return await user(req);
return new Response("garf dont know what want..."); return new Response("garf dont know what want...");
}, },
websocket: { websocket: {
message(ws, message) {}, message(ws, message) {
open(ws) {}, if (!ws.data.authenticated) ws.close();
close(ws, code, message) {}, server.publish("global", JSON.stringify({ message, identity: ws.data.identity }));
},
open(ws) {
const message = `${ws.data.identity.username} has joined the channel.`;
server.publish("global", JSON.stringify({ message, identity: terryIdentity }));
ws.subscribe("global");
},
close(ws, code, msg) {
const message = `${ws.data.identity.username} has left the channel.`;
server.publish("global", JSON.stringify({ message, identity: terryIdentity }));
ws.unsubscribe("global");
},
drain(ws) {}, drain(ws) {},
}, },
}); });

View file

@ -1,3 +1,24 @@
export default async function (req: Request): Promise<Response> { import type { Server } from "bun";
return new Response(); import { decrypt } from "../lib/encryption";
import type { PartialDiscordUser } from "./token";
interface NegotiateRequest {
id: string;
iv: string;
}
export default async function (req: Request, server: Server): Promise<void | Response> {
if (req.method !== "POST") return new Response("garf expected a POST request...");
if (!req.headers.get("Content-Type")) return new Response("garf expected some jay sawn...");
if (!req.headers.get("Content-Type")?.includes("application/json")) return new Response("garf expected some jay sawn...");
const body = (await req.json()) as NegotiateRequest;
if (!body) return new Response("garf expected some jay sawn...");
const decrypted = await decrypt(body.id, body.iv);
if (!decrypted) return new Response("invalid identity!!!!!");
const identity = JSON.parse(decrypted.decrypted) as PartialDiscordUser;
const success = server.upgrade(req, { data: { authenticated: true, identity } });
return success ? undefined : new Response("WebSocket upgrade error", { status: 400 });
} }

View file

@ -1,3 +1,5 @@
import { encrypt } from "../lib/encryption";
interface TokenRequest { interface TokenRequest {
code: string; code: string;
} }
@ -10,6 +12,12 @@ interface TokenResponse {
scope: string; scope: string;
} }
export interface PartialDiscordUser {
id: string;
username: string;
avatar: string;
}
export default async function (req: Request): Promise<Response> { export default async function (req: Request): Promise<Response> {
if (req.method !== "POST") return new Response("garf expected a POST request..."); if (req.method !== "POST") return new Response("garf expected a POST request...");
if (!req.headers.get("Content-Type")) return new Response("garf expected some jay sawn..."); if (!req.headers.get("Content-Type")) return new Response("garf expected some jay sawn...");
@ -30,13 +38,14 @@ export default async function (req: Request): Promise<Response> {
}); });
const { access_token } = (await response.json()) as TokenResponse; const { access_token } = (await response.json()) as TokenResponse;
const userResponse = await fetch("https://discord.com/api/v10/users/@me", { const userResponse = await fetch("https://discord.com/api/v10/users/@me", {
headers: { Authorization: "Bearer " + access_token }, headers: { Authorization: "Bearer " + access_token },
}); });
const user = (await userResponse.json()) as PartialDiscordUser;
const { encrypted, iv } = await encrypt(JSON.stringify(user));
console.log(await userResponse.json()); return new Response(JSON.stringify({ access_token, identity: { id: encrypted, iv } }), {
return new Response(JSON.stringify({ access_token }), {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },

3
src/routes/user.ts Normal file
View file

@ -0,0 +1,3 @@
export default async function (req: Request): Promise<Response> {
return new Response();
}