This commit is contained in:
cirroskais 2024-03-18 21:56:37 -04:00
parent a01abce47b
commit ec6f858cf2
No known key found for this signature in database
GPG key ID: 5FC73EBF2678E33D
7 changed files with 57 additions and 1 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -6,7 +6,8 @@
"build": "bun build . --compile --outfile=build/garf-api"
},
"devDependencies": {
"@types/bun": "latest"
"@types/bun": "latest",
"@types/node": "^20.11.29"
},
"peerDependencies": {
"typescript": "^5.0.0"

2
src/env.d.ts vendored
View file

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

View file

@ -1,5 +1,6 @@
import token from "./routes/token";
import config from "./routes/config";
import negotiate from "./routes/negotiate";
Bun.serve({
port: 3000,
@ -10,8 +11,15 @@ Bun.serve({
if (url.pathname === "/") return await config(req);
if (url.pathname === "/token") return await token(req);
if (url.pathname === "/negotiate") return await negotiate(req);
return new Response("garf dont know what want...");
},
websocket: {
message(ws, message) {},
open(ws) {},
close(ws, code, message) {},
drain(ws) {},
},
});
console.log("Listening on 0.0.0.0:3000");

37
src/lib/encryption.ts Normal file
View file

@ -0,0 +1,37 @@
import { createCipheriv, createDecipheriv, scrypt, randomBytes } from "node:crypto";
const KEY = ((await scryptAsync(process.env.ENCRYPTION_KEY, 32)) as Buffer).toString("hex");
function scryptAsync(password: string, length: number) {
return new Promise((resolve, reject) => {
const pwBuf = Buffer.from(password);
scrypt(pwBuf, process.env.ENCRYPTION_SALT, length, (err, key) => {
if (err) return reject(err);
resolve(key);
});
});
}
export async function encrypt(data: string) {
const iv = randomBytes(16);
const cipher = createCipheriv("aes-256-ccm", KEY, iv);
let encrypted = cipher.update(data, "utf8", "base64url");
encrypted += cipher.final("base64url");
return { encrypted, iv: iv.toString("hex") };
}
export async function decrypt(data: string, iv: string) {
const ivBuf = Buffer.from(iv, "hex");
const decipher = createDecipheriv("aes-256-ccm", KEY, ivBuf);
let decrypted = decipher.update(data, "base64url", "utf-8");
try {
decrypted += decipher.final("utf8");
} catch {
return false;
}
return { decrypted };
}

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

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

View file

@ -30,6 +30,11 @@ export default async function (req: Request): Promise<Response> {
});
const { access_token } = (await response.json()) as TokenResponse;
const userResponse = await fetch("https://discord/api/v10/user/@me", {
headers: { Authorization: "Bearer " + access_token },
});
console.log(await userResponse.json());
return new Response(JSON.stringify({ access_token }), {
headers: {