testing stuffs
This commit is contained in:
parent
451a02d2e8
commit
f5be5beb42
5 changed files with 73 additions and 11 deletions
1
src/env.d.ts
vendored
1
src/env.d.ts
vendored
|
@ -2,6 +2,7 @@ declare module "bun" {
|
|||
interface Env {
|
||||
DISCORD_CLIENT_ID: string;
|
||||
DISCORD_CLIENT_SECRET: string;
|
||||
DISCORD_CLIENT_TOKEN: string;
|
||||
ENCRYPTION_KEY: string;
|
||||
ENCRYPTION_SALT: string;
|
||||
}
|
||||
|
|
40
src/index.ts
40
src/index.ts
|
@ -1,23 +1,51 @@
|
|||
import token from "./routes/token";
|
||||
import config from "./routes/config";
|
||||
import user from "./routes/user";
|
||||
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,
|
||||
async fetch(req) {
|
||||
async fetch(req, server) {
|
||||
const url = new URL(req.url);
|
||||
|
||||
console.log(`${new Date().toUTCString()} ${req.method} ${url.pathname}`);
|
||||
|
||||
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 === "/negotiate") return await negotiate(req);
|
||||
if (url.pathname === "/user") return await user(req);
|
||||
return new Response("garf dont know what want...");
|
||||
},
|
||||
websocket: {
|
||||
message(ws, message) {},
|
||||
open(ws) {},
|
||||
close(ws, code, message) {},
|
||||
message(ws, message) {
|
||||
if (!ws.data.authenticated) ws.close();
|
||||
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) {},
|
||||
},
|
||||
});
|
||||
|
|
|
@ -1,3 +1,24 @@
|
|||
export default async function (req: Request): Promise<Response> {
|
||||
return new Response();
|
||||
import type { Server } from "bun";
|
||||
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 });
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { encrypt } from "../lib/encryption";
|
||||
|
||||
interface TokenRequest {
|
||||
code: string;
|
||||
}
|
||||
|
@ -10,6 +12,12 @@ interface TokenResponse {
|
|||
scope: string;
|
||||
}
|
||||
|
||||
export interface PartialDiscordUser {
|
||||
id: string;
|
||||
username: string;
|
||||
avatar: string;
|
||||
}
|
||||
|
||||
export default async function (req: Request): Promise<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...");
|
||||
|
@ -30,13 +38,14 @@ export default async function (req: Request): Promise<Response> {
|
|||
});
|
||||
|
||||
const { access_token } = (await response.json()) as TokenResponse;
|
||||
|
||||
const userResponse = await fetch("https://discord.com/api/v10/users/@me", {
|
||||
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 }), {
|
||||
return new Response(JSON.stringify({ access_token, identity: { id: encrypted, iv } }), {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
|
|
3
src/routes/user.ts
Normal file
3
src/routes/user.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export default async function (req: Request): Promise<Response> {
|
||||
return new Response();
|
||||
}
|
Loading…
Reference in a new issue