91 lines
2.4 KiB
TypeScript
91 lines
2.4 KiB
TypeScript
|
|
// app/api/proxy/[...path]/route.ts
|
||
|
|
// API Proxy для обращения к Marzban серверу
|
||
|
|
|
||
|
|
export async function GET(
|
||
|
|
request: Request,
|
||
|
|
{ params }: { params: { path: string[] } }
|
||
|
|
) {
|
||
|
|
const path = params.path.join('/');
|
||
|
|
const baseUrl = 'https://umbrix2.3to3.sbs';
|
||
|
|
const url = `${baseUrl}/${path}`;
|
||
|
|
|
||
|
|
console.log('[Proxy] Fetching:', url);
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await fetch(url, {
|
||
|
|
headers: {
|
||
|
|
'Accept': 'application/json',
|
||
|
|
'User-Agent': 'Umbrix-TelegramBot/1.0',
|
||
|
|
},
|
||
|
|
// Отключаем кэш для актуальных данных
|
||
|
|
cache: 'no-store',
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
console.error('[Proxy] HTTP error:', response.status, response.statusText);
|
||
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const contentType = response.headers.get('content-type');
|
||
|
|
|
||
|
|
// Если это JSON - парсим
|
||
|
|
if (contentType?.includes('application/json')) {
|
||
|
|
const data = await response.json();
|
||
|
|
console.log('[Proxy] Success (JSON):', Object.keys(data));
|
||
|
|
return Response.json(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Если это текст/HTML - возвращаем как есть
|
||
|
|
const text = await response.text();
|
||
|
|
console.log('[Proxy] Success (Text):', text.substring(0, 100));
|
||
|
|
return new Response(text, {
|
||
|
|
headers: {
|
||
|
|
'Content-Type': contentType || 'text/plain',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('[Proxy] Error:', error);
|
||
|
|
return Response.json(
|
||
|
|
{
|
||
|
|
error: 'Failed to fetch data from Marzban server',
|
||
|
|
details: error instanceof Error ? error.message : 'Unknown error',
|
||
|
|
url: url,
|
||
|
|
},
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Также поддерживаем POST для будущих API calls
|
||
|
|
export async function POST(
|
||
|
|
request: Request,
|
||
|
|
{ params }: { params: { path: string[] } }
|
||
|
|
) {
|
||
|
|
const path = params.path.join('/');
|
||
|
|
const baseUrl = 'https://umbrix2.3to3.sbs';
|
||
|
|
const url = `${baseUrl}/${path}`;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const body = await request.json();
|
||
|
|
|
||
|
|
const response = await fetch(url, {
|
||
|
|
method: 'POST',
|
||
|
|
headers: {
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
'Accept': 'application/json',
|
||
|
|
},
|
||
|
|
body: JSON.stringify(body),
|
||
|
|
});
|
||
|
|
|
||
|
|
const data = await response.json();
|
||
|
|
return Response.json(data);
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
return Response.json(
|
||
|
|
{ error: 'Failed to post data' },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|