mirror of
https://github.com/PreMiD/PreMiD.git
synced 2026-04-06 04:41:58 +02:00
Compare commits
5 Commits
api-worker
...
api-worker
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
411a70f567 | ||
|
|
4d1b092ee5 | ||
|
|
aa41f1cdae | ||
|
|
04b5d54697 | ||
|
|
fb096bc4be |
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@premid/api-master",
|
||||
"type": "module",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.4",
|
||||
"private": true,
|
||||
"description": "PreMiD's api master",
|
||||
"license": "MPL-2.0",
|
||||
|
||||
@@ -16,7 +16,7 @@ export async function clearOldSessions() {
|
||||
for (const key of sessionKeys) {
|
||||
const session = await redis.hgetall(key);
|
||||
|
||||
if (!session.t || !session.u) {
|
||||
if (!session.t || !session.u || !session.s) {
|
||||
await redis.del(key);
|
||||
cleared++;
|
||||
continue;
|
||||
@@ -32,7 +32,7 @@ export async function clearOldSessions() {
|
||||
discord.setToken(session.t);
|
||||
await discord.post("/users/@me/headless-sessions/delete", {
|
||||
body: {
|
||||
token: key.split(":")[2], // Extract session token from key
|
||||
token: session.s,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@premid/api-worker",
|
||||
"type": "module",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.7",
|
||||
"private": true,
|
||||
"description": "PreMiD's api",
|
||||
"license": "MPL-2.0",
|
||||
@@ -28,7 +28,7 @@
|
||||
"@opentelemetry/node": "^0.24.0",
|
||||
"@premid/db": "workspace:*",
|
||||
"@sentry/node": "^8.17.0",
|
||||
"arktype": "2.0.0-beta.2",
|
||||
"arktype": "2.0.0-rc.6",
|
||||
"defu": "^6.1.4",
|
||||
"discord-api-types": "^0.37.92",
|
||||
"fastify": "^4.28.1",
|
||||
|
||||
@@ -5,6 +5,7 @@ import WebSocket from "ws";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import type { RawData } from "ws";
|
||||
import { redis } from "../functions/createServer.js";
|
||||
import { shortHash } from "../functions/shortHash.js";
|
||||
import { counter } from "../tracing.js";
|
||||
|
||||
const schema = scope({
|
||||
@@ -75,14 +76,15 @@ export class Socket {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
await redis.hmset(
|
||||
`pmd:session:${this.currentSession.token}`,
|
||||
`pmd:session:${shortHash(this.currentSession.token, this.currentToken.token)}`,
|
||||
{
|
||||
t: this.currentToken.token,
|
||||
u: now,
|
||||
s: this.currentSession.token,
|
||||
},
|
||||
);
|
||||
|
||||
await redis.expire(`pmd:session:${this.currentSession.token}`, 60); // Expire after 1 minute
|
||||
await redis.expire(`pmd:session:${shortHash(this.currentSession.token, this.currentToken.token)}`, 60); // Expire after 1 minute
|
||||
}
|
||||
|
||||
async isTokenValid(token: typeof schema.token.infer) {
|
||||
|
||||
5
apps/api-worker/src/functions/shortHash.ts
Normal file
5
apps/api-worker/src/functions/shortHash.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
export function shortHash(...input: string[]) {
|
||||
return createHash("sha256").update(input.join("")).digest("hex").slice(0, 16);
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { type } from "arktype";
|
||||
import { Routes } from "discord-api-types/v10";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { redis } from "../functions/createServer.js";
|
||||
import { shortHash } from "../functions/shortHash.js";
|
||||
|
||||
const schema = type({
|
||||
token: "string.trim",
|
||||
@@ -26,15 +27,16 @@ export async function sessionKeepAlive(request: FastifyRequest, reply: FastifyRe
|
||||
const now = Math.floor(Date.now() / 1000); // Unix timestamp in seconds
|
||||
|
||||
await redis.hmset(
|
||||
`pmd:session:${out.session}`,
|
||||
`pmd:session:${shortHash(out.session, out.token)}`,
|
||||
{
|
||||
t: out.token,
|
||||
u: now,
|
||||
s: out.session,
|
||||
},
|
||||
);
|
||||
|
||||
// Set expiration for the hash
|
||||
await redis.expire(`pmd:session:${out.session}`, 60); // Expire after 1 minute
|
||||
await redis.expire(`pmd:session:${shortHash(out.session, out.token)}`, 60); // Expire after 1 minute
|
||||
|
||||
const interval = Number.parseInt(process.env.SESSION_KEEP_ALIVE_INTERVAL ?? "5000");
|
||||
|
||||
|
||||
2387
pnpm-lock.yaml
generated
2387
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user