new commit
This commit is contained in:
64
controllers/db.js
Normal file
64
controllers/db.js
Normal file
@@ -0,0 +1,64 @@
|
||||
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
|
||||
)`)
|
||||
}
|
||||
|
||||
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] <Ban:Error>")
|
||||
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] <Unban:Warn>")
|
||||
logger.log(`No launcher with this hwid<${hardware_id}> is banned`)
|
||||
}
|
||||
return true
|
||||
} catch (err) {
|
||||
logger.error("[DATABASE] <Unban:Error>")
|
||||
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)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initDB,
|
||||
getBan,
|
||||
banLauncher,
|
||||
unbanLauncher
|
||||
}
|
||||
22
controllers/fileHash.js
Normal file
22
controllers/fileHash.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const fs = require("node:fs")
|
||||
const crypto = require("node:crypto")
|
||||
|
||||
function getFileHash(filePath) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const hash = crypto.createHash("sha256")
|
||||
const file = fs.createReadStream(filePath)
|
||||
file.on("error", (err) => {
|
||||
reject(err)
|
||||
})
|
||||
file.on("data", (chunk) => {
|
||||
hash.update(chunk)
|
||||
})
|
||||
file.on("end", () => {
|
||||
resolve(hash.digest("hex"))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFileHash
|
||||
}
|
||||
Reference in New Issue
Block a user