const mysql = require('mysql2/promise'); const dotenv = require('dotenv'); dotenv.config(); const dbConfig = { host: process.env.DB_HOST || 'localhost', port: process.env.DB_PORT || 3306, user: process.env.DB_USER || 'root', password: process.env.DB_PASSWORD || '', database: process.env.DB_NAME || 'webssh' }; // Create connection pool const pool = mysql.createPool({ ...dbConfig, waitForConnections: true, connectionLimit: 10, queueLimit: 0 }); // Test connection const authenticate = async () => { try { const connection = await pool.getConnection(); console.log('Database connected successfully'); connection.release(); // Initialize database tables await initializeTables(); return true; } catch (error) { console.error('Database connection failed:', error); return false; } }; // Initialize database tables const initializeTables = async () => { try { // Users table await pool.execute(` CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, phone VARCHAR(20), password_hash VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) `); // Groups table await pool.execute(` CREATE TABLE IF NOT EXISTS \`groups\` ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, name VARCHAR(255) NOT NULL, description TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, UNIQUE KEY unique_user_group (user_id, name) ) `); // Servers table (with encrypted credentials) await pool.execute(` CREATE TABLE IF NOT EXISTS servers ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, name VARCHAR(255) NOT NULL, host VARCHAR(255) NOT NULL, port INT DEFAULT 22, username VARCHAR(255) NOT NULL, password_encrypted TEXT, private_key_encrypted TEXT, auth_type ENUM('password', 'key') DEFAULT 'password', group_id INT, group_name VARCHAR(255) DEFAULT 'Default', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (group_id) REFERENCES \`groups\`(id) ON DELETE SET NULL ) `); // Connection logs table await pool.execute(` CREATE TABLE IF NOT EXISTS connection_logs ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, server_id INT, host VARCHAR(255) NOT NULL, start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, end_time TIMESTAMP NULL, duration INT DEFAULT 0, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE SET NULL ) `); console.log('Database tables initialized successfully'); } catch (error) { console.error('Error initializing database tables:', error); } }; module.exports = { pool, authenticate, initializeTables };