mirror of
https://github.com/PreMiD/PreMiD.git
synced 2026-04-06 04:41:58 +02:00
Compare commits
2 Commits
api-master
...
api-master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9db9e931b6 | ||
|
|
665263e9b5 |
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@premid/api-master",
|
||||
"type": "module",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.6",
|
||||
"private": true,
|
||||
"description": "PreMiD's api master",
|
||||
"license": "MPL-2.0",
|
||||
|
||||
@@ -2,37 +2,35 @@ import { REST } from "@discordjs/rest";
|
||||
import { mainLog, redis } from "../index.js";
|
||||
|
||||
export async function clearOldSessions() {
|
||||
const sessionKeys = await redis.keys("pmd:session:*");
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const sessions = await redis.hgetall("pmd-api.sessions");
|
||||
const now = Date.now();
|
||||
|
||||
if (sessionKeys.length === 0) {
|
||||
if (Object.keys(sessions).length === 0) {
|
||||
mainLog("No sessions to clear");
|
||||
return;
|
||||
}
|
||||
|
||||
mainLog(`Checking ${sessionKeys.length} sessions`);
|
||||
mainLog(`Checking ${Object.keys(sessions).length} sessions`);
|
||||
|
||||
let cleared = 0;
|
||||
for (const key of sessionKeys) {
|
||||
const session = await redis.hgetall(key);
|
||||
for (const [key, value] of Object.entries(sessions)) {
|
||||
const session = JSON.parse(value) as {
|
||||
token: string;
|
||||
session: string;
|
||||
lastUpdated: number;
|
||||
};
|
||||
|
||||
if (!session.t || !session.u || !session.s) {
|
||||
await redis.del(key);
|
||||
cleared++;
|
||||
continue;
|
||||
}
|
||||
|
||||
//* If the session is younger than 30 seconds, skip it
|
||||
if (now - Number(session.u) < 30)
|
||||
// ? If the session is younger than 30seconds, skip it
|
||||
if (now - session.lastUpdated < 30000)
|
||||
continue;
|
||||
|
||||
//* Delete the session
|
||||
try {
|
||||
const discord = new REST({ version: "10", authPrefix: "Bearer" });
|
||||
discord.setToken(session.t);
|
||||
discord.setToken(session.token);
|
||||
await discord.post("/users/@me/headless-sessions/delete", {
|
||||
body: {
|
||||
token: session.s,
|
||||
token: session.session,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -41,7 +39,7 @@ export async function clearOldSessions() {
|
||||
}
|
||||
|
||||
cleared++;
|
||||
await redis.del(key);
|
||||
await redis.hdel("pmd-api.sessions", key);
|
||||
}
|
||||
|
||||
mainLog(`Cleared ${cleared} sessions`);
|
||||
|
||||
@@ -5,7 +5,6 @@ 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({
|
||||
@@ -55,7 +54,7 @@ export class Socket {
|
||||
break;
|
||||
}
|
||||
case "session": {
|
||||
await redis.del(`pmd:session:${out.token}`);
|
||||
await redis.hdel("pmd-api.sessions", out.token);
|
||||
this.currentSession = out;
|
||||
break;
|
||||
}
|
||||
@@ -73,18 +72,15 @@ export class Socket {
|
||||
if (!this.currentToken || !this.currentSession)
|
||||
return;
|
||||
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
await redis.hmset(
|
||||
`pmd:session:${shortHash(this.currentSession.token, this.currentToken.token)}`,
|
||||
{
|
||||
t: this.currentToken.token,
|
||||
u: now,
|
||||
s: this.currentSession.token,
|
||||
},
|
||||
await redis.hset(
|
||||
"pmd-api.sessions",
|
||||
this.currentSession.token,
|
||||
JSON.stringify({
|
||||
session: this.currentSession.token,
|
||||
token: this.currentToken.token,
|
||||
lastUpdated: Date.now(),
|
||||
}),
|
||||
);
|
||||
|
||||
await redis.expire(`pmd:session:${shortHash(this.currentSession.token, this.currentToken.token)}`, 60); // Expire after 1 minute
|
||||
}
|
||||
|
||||
async isTokenValid(token: typeof schema.token.infer) {
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
export function shortHash(...input: string[]) {
|
||||
return createHash("sha256").update(input.join("")).digest("hex").slice(0, 16);
|
||||
}
|
||||
@@ -4,7 +4,6 @@ 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",
|
||||
@@ -24,20 +23,16 @@ export async function sessionKeepAlive(request: FastifyRequest, reply: FastifyRe
|
||||
if (!await isTokenValid(out.token))
|
||||
return reply.status(400).send({ code: "INVALID_TOKEN", message: "The token is invalid" });
|
||||
|
||||
const now = Math.floor(Date.now() / 1000); // Unix timestamp in seconds
|
||||
|
||||
await redis.hmset(
|
||||
`pmd:session:${shortHash(out.session, out.token)}`,
|
||||
{
|
||||
t: out.token,
|
||||
u: now,
|
||||
s: out.session,
|
||||
},
|
||||
await redis.hset(
|
||||
"pmd-api.sessions",
|
||||
out.token,
|
||||
JSON.stringify({
|
||||
session: out.session,
|
||||
token: out.token,
|
||||
lastUpdated: Date.now(),
|
||||
}),
|
||||
);
|
||||
|
||||
// Set expiration for the hash
|
||||
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");
|
||||
|
||||
return reply.status(200).send({
|
||||
|
||||
Reference in New Issue
Block a user