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;
}
}

View File

@@ -6,9 +6,11 @@ import { useEffect, useState } from "react";
import { getUrl } from "./actions";
export default function Home() {
const [url, setUrl] = useState("");
const initData = useRawInitData();
const onCopyClick = async () => {
await navigator.clipboard.writeText(window.location.href);
if (!url.length) return;
await navigator.clipboard.writeText(url);
setIsCopied(true);
setTimeout(() => {
setIsCopied(false);
@@ -17,8 +19,12 @@ export default function Home() {
const [isCopied, setIsCopied] = useState(false);
useEffect(() => {
const fetchData = async () => {
const data = await getUrl(initData);
console.log(data);
try {
const data = await getUrl(initData);
setUrl(data);
} catch (e) {
console.error(e);
}
};
fetchData();
}, [initData]);

13
src/lib/url.ts Normal file
View File

@@ -0,0 +1,13 @@
export function getValidUrl({ email, id }: { email: string; id: string }) {
const obj = {
fp: "chrome",
alpn: "h2,h3",
packetEncoding: "xudp",
security: "tls",
type: "ws",
path: "/myverysecretpath",
};
const params = new URLSearchParams(obj).toString();
const url = `vless://${id}@nwaifu.su:443?${params}#WS-${email}`;
return url;
}

3
src/utils/redis.ts Normal file
View File

@@ -0,0 +1,3 @@
import Redis from "ioredis";
export const redis = new Redis(process.env.REDIS_URL || "redis://localhost:6379");