const DataBase = require("better-sqlite3") const logger = require("../modules/logger") const path = require("node:path") const db = new DataBase(path.join(__dirname, "..", "data", "main.db")) function initDB() { db.exec(` CREATE TABLE IF NOT EXISTS banned ( banned_at TEXT DEFAULT CURRENT_TIMESTAMP, last_minecraft_uuid TEXT, hardware_id TEXT, banned_by TEXT, remote_ip TEXT, reason TEXT ) `) db.exec(` CREATE TABLE IF NOT EXISTS api_keys ( id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT NOT NULL, delivered_by TEXT NOT NULL, delivered_at TEXT DEFAULT CURRENT_TIMESTAMP ) `) db.exec(` CREATE TABLE IF NOT EXISTS launchers ( hardware_id TEXT NOT NULL, last_minecraft_uuid TEXT NOT NULL ) `) } function banLauncher(last_minecraft_uuid, hardware_id, banned_by, remote_ip, reason) { const query = ` INSERT INTO banned (last_minecraft_uuid, hardware_id, banned_by, remote_ip, reason) VALUES (?, ?, ?, ?, ?) ` try { const stmt = db.prepare(query) const result = stmt.run(last_minecraft_uuid, hardware_id, banned_by, remote_ip, reason) return true } catch (error) { logger.error("[DATABASE] ") logger.error(error) return false } } function unbanLauncher(hardware_id) { const sql = `DELETE FROM banned WHERE hardware_id = ?` try { const stmt = db.prepare(sql) const result = stmt.run(hardware_id) if (result.changes === 0) { logger.log("[DATABASE] ") logger.log(`No launcher with this hwid<${hardware_id}> is banned`) } return true } catch (err) { logger.error("[DATABASE] ") logger.error(error) } } function getBan(hardware_id) { const query = 'SELECT * FROM banned WHERE hardware_id = ?' const stmt = db.prepare(query) return stmt.get(hardware_id) } function isTokenValid(token) { const query = 'SELECT * FROM api_keys WHERE key = ?' const stmt = db.prepare(query) const result = stmt.get(token) return !!result } function createToken(key, delivered_by) { const query = ` INSERT INTO api_keys (key, delivered_by) VALUES (?, ?) ` try { const stmt = db.prepare(query) stmt.run(key, delivered_by) return true } catch (error) { logger.error("[DATABASE] ") logger.error(error) return false } } function revokeToken(key) { const query = `DELETE FROM api_keys WHERE key = ?` try { const stmt = db.prepare(query) const result = stmt.run(key) if (result.changes === 0) { logger.log("[DATABASE] ") logger.log(`No token with this key<${key}> exists`) } return true } catch (error) { logger.error("[DATABASE] ") logger.error(error) return false } } function regenerateToken(oldKey, newKey) { const query = `UPDATE api_keys SET key = ? WHERE key = ?` try { const stmt = db.prepare(query) const result = stmt.run(newKey, oldKey) if (result.changes === 0) { logger.log("[DATABASE] ") logger.log(`No token with this key<${oldKey}> exists`) } return true } catch (error) { logger.error("[DATABASE] ") logger.error(error) return false } } function setLastUUIDForLauncher(hardware_id, last_minecraft_uuid) { const query = ` INSERT INTO launchers (hardware_id, last_minecraft_uuid) VALUES (?, ?) ` try { const stmt = db.prepare(query) const result = stmt.run(hardware_id, last_minecraft_uuid) return true } catch (error) { logger.error("[DATABASE] ") logger.error(error) return false } } module.exports = { initDB, getBan, banLauncher, unbanLauncher, isTokenValid, createToken, revokeToken, regenerateToken, setLastUUIDForLauncher }