67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import decrypt from "@moduletrace/datasquirel/dist/package-shared/functions/dsql/decrypt";
|
|
|
|
const AuthServerPort = 3177;
|
|
|
|
type AdminPagesAuth = {
|
|
date: number;
|
|
user_id: number;
|
|
url?: string;
|
|
};
|
|
|
|
const AuthExpiryTime = 1000 * 60 * 60;
|
|
|
|
const server = Bun.serve({
|
|
port: AuthServerPort,
|
|
hostname: "0.0.0.0",
|
|
fetch(req) {
|
|
try {
|
|
const reqURL = new URL(req.url);
|
|
|
|
if (reqURL.pathname !== "/") {
|
|
return new Response("Auth Success!", {
|
|
status: 200,
|
|
});
|
|
}
|
|
|
|
const host = req.headers.get("host");
|
|
const query = req.headers.get("x-original-uri");
|
|
const href = `https://${host}${query}`;
|
|
const url = new URL(href);
|
|
|
|
const key = url.searchParams.get("key");
|
|
|
|
const decryptedKey = JSON.parse(
|
|
decrypt({ encryptedString: key! }),
|
|
) as AdminPagesAuth;
|
|
|
|
const decrptedUrl = decryptedKey.url;
|
|
|
|
if (!host || !decrptedUrl) {
|
|
throw new Error(`Origin Not Found!`);
|
|
}
|
|
|
|
if (decrptedUrl !== host) {
|
|
throw new Error(`Host Mismatch!`);
|
|
}
|
|
|
|
const dateIssued = decryptedKey.date;
|
|
|
|
if (Date.now() - dateIssued > AuthExpiryTime) {
|
|
throw new Error(`Key Expired!`);
|
|
}
|
|
|
|
return new Response("Auth Success!", {
|
|
status: 200,
|
|
});
|
|
} catch (error: any) {
|
|
console.log(`Auth Server Error => ${error.message}`);
|
|
|
|
return new Response("Auth Failed!", {
|
|
status: 401,
|
|
});
|
|
}
|
|
},
|
|
});
|
|
|
|
console.log(`Auth Server running at http://localhost:${server.port}/ ...`);
|