feat: redis and valid url

This commit is contained in:
2025-06-09 11:07:29 +03:00
parent 11c6d538b1
commit f919b6b59a
7 changed files with 87 additions and 21 deletions

View File

@@ -1,27 +1,32 @@
"use server";
import { API_LINK, XUIApiLinks } from "@/lib/enum";
import { authFetch } from "@/lib/login";
import { getValidUrl } from "@/lib/url";
import { prisma } from "@/utils/prisma";
import { redis } from "@/utils/redis";
import { parse, validate } from "@telegram-apps/init-data-node";
import { ClientSettings, InboundResponse } from "./_dto/inbounds";
//TODO: Make it valid proxy url
async function getUrlApi(email: string) {
async function getInboundApi() {
const res = await authFetch(API_LINK + XUIApiLinks.GET_INBOUNDS);
const data: InboundResponse = await res.json();
const inbound = data.obj.find((inbound) => inbound.remark === "WS");
return inbound;
}
async function getUrlApi(email: string) {
const cachedInbound = await redis.get("inbound");
const inbound = cachedInbound ? JSON.parse(cachedInbound) : await getInboundApi();
if (!inbound) {
return Response.json(
{
success: false,
error: "Inbound not found",
},
{ status: 404 }
);
throw new Error("Inbound not found");
}
await redis.set("inbound", JSON.stringify(inbound), "EX", 3600);
const users: ClientSettings = JSON.parse(inbound.settings);
const user = users.clients.find((user) => user.email === email);
return user;
if (!user) {
throw new Error("User not found");
}
return getValidUrl({ email, id: user.id });
}
export async function getUrl(initData: string = "") {
try {
@@ -30,13 +35,23 @@ export async function getUrl(initData: string = "") {
expiresIn: 3600,
});
const initDataParsed = parse(initData);
const user = await prisma.user.findFirst({
where: {
tgId: initDataParsed.user ? initDataParsed.user.id.toString() : "0",
},
});
return await getUrlApi(user?.email || "");
if (!initDataParsed.user) {
throw new Error("User not found");
}
const cachedUser = await redis.get(`user:${initDataParsed.user.id}`);
const user = cachedUser
? JSON.parse(cachedUser)
: await prisma.user.findFirst({
where: {
tgId: initDataParsed.user.id.toString(),
},
});
if (!user) {
throw new Error("User not found");
}
await redis.set(`user:${initDataParsed.user.id}`, JSON.stringify(user), "EX", 60);
return await getUrlApi(user.email || "");
} catch (e) {
console.log(e);
throw e;
}
}