2025-06-28 17:07:53 +00:00
|
|
|
import { DB_PATH } from "$lib/utils/constants";
|
2025-06-30 11:55:29 +00:00
|
|
|
import { logger } from "$lib/utils/logger";
|
2025-06-28 17:19:40 +00:00
|
|
|
import { Database, Statement } from "bun:sqlite";
|
2025-06-28 17:07:53 +00:00
|
|
|
|
2025-06-28 17:19:40 +00:00
|
|
|
export class SSLSnifferApp {
|
2025-06-28 17:07:53 +00:00
|
|
|
|
|
|
|
|
private static initialized = false
|
|
|
|
|
|
2025-06-28 21:52:44 +00:00
|
|
|
private static db: Database
|
2025-06-28 17:07:53 +00:00
|
|
|
|
|
|
|
|
// UGLY: should be more flexible
|
2025-06-28 21:52:44 +00:00
|
|
|
public static init() {
|
|
|
|
|
|
2025-06-30 11:55:29 +00:00
|
|
|
logger.debug("Initializing Database", "SSLSnifferApp")
|
|
|
|
|
|
2025-06-28 21:52:44 +00:00
|
|
|
if (SSLSnifferApp.initialized) {
|
2025-06-30 11:55:29 +00:00
|
|
|
logger.debug("database initialized Twice?", "SSLSnifferApp")
|
2025-06-28 21:52:44 +00:00
|
|
|
throw new Error("SSLSniffer has already been initialized")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SSLSnifferApp.db = SSLSnifferApp.initDatabase()
|
2025-06-28 17:19:40 +00:00
|
|
|
SSLSnifferApp.initialized = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-06-28 21:52:44 +00:00
|
|
|
public static prepare(query: string) {
|
|
|
|
|
return SSLSnifferApp.db.prepare(query)
|
2025-06-28 17:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static initDatabase() {
|
|
|
|
|
|
|
|
|
|
const db = new Database(
|
|
|
|
|
DB_PATH,
|
|
|
|
|
{
|
|
|
|
|
create: true,
|
|
|
|
|
strict: true
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Improve performance
|
|
|
|
|
db.exec("PRAGMA journal_mode = WAL;");
|
2025-06-29 11:03:05 +00:00
|
|
|
// Activate cascade operations
|
|
|
|
|
db.exec("PRAGMA foreign_keys = ON")
|
2025-06-28 17:07:53 +00:00
|
|
|
return db
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 21:52:44 +00:00
|
|
|
private static closeDatabase(graceful?: boolean) {
|
|
|
|
|
|
2025-06-28 17:07:53 +00:00
|
|
|
if (!graceful) {
|
2025-06-28 21:52:44 +00:00
|
|
|
graceful = true
|
2025-06-28 17:07:53 +00:00
|
|
|
}
|
|
|
|
|
// Change of variable to make it more readable
|
|
|
|
|
const forceful = !graceful
|
|
|
|
|
|
2025-06-28 21:52:44 +00:00
|
|
|
SSLSnifferApp.db.close(forceful)
|
2025-06-28 17:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|