Initial commit - cleaned for CV

This commit is contained in:
Renato97
2026-03-31 01:23:33 -03:00
commit 9c11f23af0
142 changed files with 13690 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
import { NextResponse } from 'next/server';
import { findUser, verifyPassword, createSession } from '@/lib/auth';
import { generateOTP } from '@/lib/otp';
import TelegramBot from 'node-telegram-bot-api';
const BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
async function sendTelegramOTP(chatId: string, otp: string) {
if (!BOT_TOKEN) return false;
try {
const bot = new TelegramBot(BOT_TOKEN, { polling: false });
await bot.sendMessage(chatId, `🔐 *Código de Acceso Finanzas*\n\nTu código es: \`${otp}\`\n\nSi no intentaste ingresar, ignora este mensaje.`, { parse_mode: 'Markdown' });
return true;
} catch (e) {
console.error('Telegram send error:', e);
return false;
}
}
export async function POST(req: Request) {
try {
const { username, password } = await req.json();
const ip = req.headers.get('x-forwarded-for')?.split(',')[0].trim() || 'unknown';
const user = findUser(username);
if (!user || !(await verifyPassword(password, user.passwordHash))) {
return NextResponse.json({ error: 'Credenciales inválidas' }, { status: 401 });
}
// Check IP
const isKnownIp = user.knownIps.includes(ip);
if (isKnownIp) {
// Login success directly
await createSession(user);
return NextResponse.json({ success: true, requireOtp: false });
} else {
// Require OTP
const otp = generateOTP(user.username);
const sent = await sendTelegramOTP(user.chatId, otp);
if (!sent) {
return NextResponse.json({ error: 'No se pudo enviar el código OTP a Telegram' }, { status: 500 });
}
return NextResponse.json({ success: true, requireOtp: true });
}
} catch (error) {
console.error('Login error:', error);
return NextResponse.json({ error: 'Error interno' }, { status: 500 });
}
}

View File

@@ -0,0 +1,7 @@
import { NextResponse } from 'next/server';
import { destroySession } from '@/lib/auth';
export async function POST() {
destroySession();
return NextResponse.json({ success: true });
}

View File

@@ -0,0 +1,41 @@
import { NextResponse } from 'next/server';
import { saveUser, findUser, hashPassword, createSession } from '@/lib/auth';
import { randomUUID } from 'crypto';
export async function POST(req: Request) {
try {
const { username, password, chatId } = await req.json();
if (!username || !password || !chatId) {
return NextResponse.json({ error: 'Faltan datos requeridos' }, { status: 400 });
}
if (findUser(username)) {
return NextResponse.json({ error: 'El usuario ya existe' }, { status: 409 });
}
// Hash password
const passwordHash = await hashPassword(password);
// Get IP
const ip = req.headers.get('x-forwarded-for') || '127.0.0.1';
const newUser = {
id: randomUUID(),
username,
passwordHash,
chatId,
knownIps: [ip] // Register current IP as known initially
};
saveUser(newUser);
// Auto login after register
await createSession(newUser);
return NextResponse.json({ success: true });
} catch (error) {
console.error('Register error:', error);
return NextResponse.json({ error: 'Error interno del servidor' }, { status: 500 });
}
}

View File

@@ -0,0 +1,34 @@
import { NextResponse } from 'next/server';
import { findUser, saveUser, createSession } from '@/lib/auth';
import { verifyOTP } from '@/lib/otp';
export async function POST(req: Request) {
try {
const { username, otp } = await req.json();
const ip = req.headers.get('x-forwarded-for')?.split(',')[0].trim() || 'unknown';
if (!verifyOTP(username, otp)) {
return NextResponse.json({ error: 'Código inválido o expirado' }, { status: 401 });
}
const user = findUser(username);
if (!user) {
return NextResponse.json({ error: 'Usuario no encontrado' }, { status: 404 });
}
// Add IP to known list if not exists
if (!user.knownIps.includes(ip) && ip !== 'unknown') {
user.knownIps.push(ip);
saveUser(user);
}
// Login success
await createSession(user);
return NextResponse.json({ success: true });
} catch (error) {
console.error('OTP Verify error:', error);
return NextResponse.json({ error: 'Error interno' }, { status: 500 });
}
}

View File

@@ -0,0 +1,48 @@
import { NextResponse } from 'next/server'
export async function POST(request: Request) {
try {
const { endpoint, token } = await request.json()
if (!endpoint || !token) {
return NextResponse.json({ success: false, error: 'Faltan datos' }, { status: 400 })
}
// Try standard /v1/models endpoint
// If user provided "https://api.example.com/v1", we append "/models"
let targetUrl = endpoint
if (targetUrl.endsWith('/')) {
targetUrl = `${targetUrl}v1/models`
} else if (!targetUrl.endsWith('/models')) {
targetUrl = `${targetUrl}/v1/models`
}
const response = await fetch(targetUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'x-api-key': token
}
})
if (!response.ok) {
const text = await response.text()
return NextResponse.json({ success: false, error: text }, { status: response.status })
}
const data = await response.json()
// Normalizing response: OpenAI/Anthropic usually return { data: [{ id: 'model-name' }, ...] }
let models: string[] = []
if (Array.isArray(data.data)) {
models = data.data.map((m: any) => m.id)
} else if (Array.isArray(data)) {
models = data.map((m: any) => m.id || m.model || m)
}
return NextResponse.json({ success: true, models })
} catch (error: any) {
return NextResponse.json({ success: false, error: error.message }, { status: 500 })
}
}

48
app/api/settings/route.ts Normal file
View File

@@ -0,0 +1,48 @@
import { NextResponse } from 'next/server'
import fs from 'fs'
import path from 'path'
import { AppSettings } from '@/lib/types'
const SETTINGS_FILE = path.join(process.cwd(), 'server-settings.json')
const DEFAULT_SETTINGS: AppSettings = {
telegram: {
botToken: '',
chatId: '',
},
aiProviders: [],
}
export async function GET() {
try {
if (!fs.existsSync(SETTINGS_FILE)) {
return NextResponse.json(DEFAULT_SETTINGS)
}
const data = fs.readFileSync(SETTINGS_FILE, 'utf8')
const settings = JSON.parse(data)
return NextResponse.json(settings)
} catch (error) {
console.error('Error reading settings:', error)
return NextResponse.json(DEFAULT_SETTINGS, { status: 500 })
}
}
export async function POST(request: Request) {
try {
const body = await request.json()
// Basic validation could go here
const settings: AppSettings = {
telegram: {
botToken: body.telegram?.botToken || '',
chatId: body.telegram?.chatId || ''
},
aiProviders: Array.isArray(body.aiProviders) ? body.aiProviders : []
}
fs.writeFileSync(SETTINGS_FILE, JSON.stringify(settings, null, 2))
return NextResponse.json({ success: true, settings })
} catch (error) {
console.error('Error saving settings:', error)
return NextResponse.json({ success: false, error: 'Failed to save settings' }, { status: 500 })
}
}

28
app/api/sync/route.ts Normal file
View File

@@ -0,0 +1,28 @@
import { NextResponse } from 'next/server';
import { getDatabase, saveDatabase } from '@/lib/server-db';
import { verifySession } from '@/lib/auth';
export async function GET() {
const session = await verifySession();
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const data = getDatabase(session.username);
return NextResponse.json(data);
}
export async function POST(req: Request) {
const session = await verifySession();
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
try {
const body = await req.json();
saveDatabase(session.username, body);
return NextResponse.json({ success: true });
} catch (error) {
return NextResponse.json({ error: 'Invalid Data' }, { status: 400 });
}
}

59
app/api/test/ai/route.ts Normal file
View File

@@ -0,0 +1,59 @@
import { NextResponse } from 'next/server'
export async function POST(request: Request) {
try {
const { endpoint, token, model } = await request.json()
if (!endpoint || !token) {
return NextResponse.json(
{ success: false, error: 'Faltan credenciales (Endpoint o Token)' },
{ status: 400 }
)
}
// Prepare target URL
let targetUrl = endpoint
if (!targetUrl.endsWith('/messages') && !targetUrl.endsWith('/chat/completions')) {
targetUrl = targetUrl.endsWith('/') ? `${targetUrl}v1/messages` : `${targetUrl}/v1/messages`
}
const start = Date.now()
// Payload for Anthropic /v1/messages
const body = {
model: model || "gpt-3.5-turbo", // Fallback if no model selected
messages: [{ role: "user", content: "Ping" }],
max_tokens: 10
}
const response = await fetch(targetUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': token,
'anthropic-version': '2023-06-01',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(body)
})
const duration = Date.now() - start
if (!response.ok) {
const text = await response.text()
return NextResponse.json(
{ success: false, error: `Error ${response.status}: ${text.slice(0, 100)}` },
{ status: response.status }
)
}
return NextResponse.json({ success: true, latency: duration })
} catch (error: any) {
console.error('AI Test Error:', error)
return NextResponse.json(
{ success: false, error: error.message || 'Error de conexión' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,45 @@
import { NextResponse } from 'next/server'
export async function POST(request: Request) {
try {
const { botToken, chatId } = await request.json()
if (!botToken || !chatId) {
return NextResponse.json(
{ success: false, error: 'Faltan credenciales (Token o Chat ID)' },
{ status: 400 }
)
}
const message = "🤖 *Prueba de Conexión*\n\n¡Hola! Si lees esto, tu bot de Finanzas está correctamente configurado. 🚀"
const url = `https://api.telegram.org/bot${botToken}/sendMessage`
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
text: message,
parse_mode: 'Markdown'
})
})
const data = await response.json()
if (!data.ok) {
return NextResponse.json(
{ success: false, error: data.description || 'Error desconocido de Telegram' },
{ status: 500 }
)
}
return NextResponse.json({ success: true, data })
} catch (error: any) {
console.error('Telegram Test Error:', error)
return NextResponse.json(
{ success: false, error: error.message || 'Error interno del servidor' },
{ status: 500 }
)
}
}