feat: started working with api
This commit is contained in:
42
src/app/actions.ts
Normal file
42
src/app/actions.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
"use server";
|
||||
import { API_LINK, XUIApiLinks } from "@/lib/enum";
|
||||
import { authFetch } from "@/lib/login";
|
||||
import { prisma } from "@/utils/prisma";
|
||||
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) {
|
||||
const res = await authFetch(API_LINK + XUIApiLinks.GET_INBOUNDS);
|
||||
const data: InboundResponse = await res.json();
|
||||
const inbound = data.obj.find((inbound) => inbound.remark === "WS");
|
||||
if (!inbound) {
|
||||
return Response.json(
|
||||
{
|
||||
success: false,
|
||||
error: "Inbound not found",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
const users: ClientSettings = JSON.parse(inbound.settings);
|
||||
const user = users.clients.find((user) => user.email === email);
|
||||
return user;
|
||||
}
|
||||
export async function getUrl(initData: string = "") {
|
||||
try {
|
||||
if (process.env.NODE_ENV === "production")
|
||||
validate(initData, process.env.BOT_TOKEN || "", {
|
||||
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 || "");
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user