new commit
This commit is contained in:
parent
671ee0cccc
commit
e7c8f811af
5
config.json
Normal file
5
config.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"web": {
|
||||
"port": 8535
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
BIN
data/main.db
Normal file
BIN
data/main.db
Normal file
Binary file not shown.
22
modules/logger.js
Normal file
22
modules/logger.js
Normal file
@ -0,0 +1,22 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
require("colors")
|
||||
|
||||
if (!fs.existsSync(path.join(__dirname, "..", "logs"))) {
|
||||
fs.mkdirSync(path.join(__dirname, "..", "logs"))
|
||||
}
|
||||
|
||||
function log(message) {
|
||||
console.log(`[${new Date().toISOString().magenta}] [${"INFO".green}] : ${message}`)
|
||||
fs.appendFileSync(path.join(__dirname, "..", "logs", "info.log") ,`[${new Date().toISOString()}] [INFO] :\r\n${message}\r\n`)
|
||||
}
|
||||
|
||||
function error(message) {
|
||||
console.error(`[${new Date().toISOString().magenta}] [${"ERROR".red}] : ${message}`)
|
||||
fs.appendFileSync(path.join(__dirname, "..", "logs", "error.log") ,`[${new Date().toISOString()}] [ERROR] :\r\n${message}\r\n`)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
error,
|
||||
log
|
||||
}
|
||||
11
modules/utils.js
Normal file
11
modules/utils.js
Normal file
@ -0,0 +1,11 @@
|
||||
function handleError(req, res, code, cause) {
|
||||
if (req.headers["accept"]?.includes("application/json")) {
|
||||
return res.status(code).json({ success: false, error: { code, message: cause } })
|
||||
} else {
|
||||
return res.render("error", { error: { code, message: cause } })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
handleError
|
||||
}
|
||||
1708
package-lock.json
generated
Normal file
1708
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
29
package.json
Normal file
29
package.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "catboat-launcher-api",
|
||||
"version": "0.0.1-alpha",
|
||||
"description": "A simple api to manage launcher",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"start": "node .",
|
||||
"start:dev": "nodemon ."
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://gitea.azures.fr/azures04/CatBoat-Launcher-API"
|
||||
},
|
||||
"author": {
|
||||
"email": "gilleslazure04@gmail.com",
|
||||
"name": "Gilles Lazure <azures04>",
|
||||
"url": "https://gitea.azures.fr"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"better-sqlite3": "^11.9.1",
|
||||
"colors": "^1.4.0",
|
||||
"ejs": "^3.1.10",
|
||||
"express": "^5.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.1.10"
|
||||
}
|
||||
}
|
||||
25
routes/api/v1/ban/index.js
Normal file
25
routes/api/v1/ban/index.js
Normal file
@ -0,0 +1,25 @@
|
||||
const utils = require("../../../../modules/utils")
|
||||
const logger = require("../../../../modules/logger")
|
||||
const controller = require("../../../../controllers/db")
|
||||
const express = require("express")
|
||||
const router = express()
|
||||
|
||||
router.post("/iam", (req, res) => {
|
||||
if (!req.body || !req.body.hwid) {
|
||||
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress
|
||||
utils.handleError(req, res, 422, "Paramètre manquant dans le corp de la requête : <hwid>")
|
||||
logger.log(`{Route:"/api/v1/ban/iam"} Strange request from ip<${ip}>`)
|
||||
} else {
|
||||
const potentialBan = controller.getBan(req.body.hwid)
|
||||
if (potentialBan != undefined) {
|
||||
delete potentialBan.remote_ip
|
||||
delete potentialBan.hardware_id
|
||||
delete potentialBan.last_minecraft_uuid
|
||||
res.status(200).json(potentialBan)
|
||||
} else {
|
||||
res.status(200).json({ sucess: true })
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
17
routes/api/v1/file/hash.js
Normal file
17
routes/api/v1/file/hash.js
Normal file
@ -0,0 +1,17 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
const controller = require("../../../../controllers/fileHash")
|
||||
const utils = require("../../../../modules/utils")
|
||||
const express = require("express")
|
||||
const router = express()
|
||||
|
||||
router.use("", (req, res) => {
|
||||
const finalPath = path.join(__dirname, "..", "..", "..", "..", "data", "gameFiles", req.path)
|
||||
if (fs.existsSync(finalPath)) {
|
||||
res.status(200).json(controller.getFileHash(finalPath))
|
||||
} else {
|
||||
utils.handleError(req, res, 404, "Fichier introuvable.")
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
33
server.js
Normal file
33
server.js
Normal file
@ -0,0 +1,33 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
const logger = require("./modules/logger")
|
||||
const config = require("./config.json")
|
||||
const express = require("express")
|
||||
const dbController = require("./controllers/db")
|
||||
const app = express()
|
||||
|
||||
app.use(express.json())
|
||||
app.use(express.urlencoded({ extended: true }))
|
||||
app.set("view engine", "ejs")
|
||||
app.set("views", path.join(__dirname, "static", "views"))
|
||||
|
||||
dbController.initDB()
|
||||
|
||||
const routes = fs.readdirSync(path.join(__dirname, "routes"), { recursive: true })
|
||||
|
||||
for (let route of routes) {
|
||||
if (route.endsWith(".js")) {
|
||||
if (route.endsWith("index.js")) {
|
||||
route = "/" + route.replace("index.js", "/").replace(/\\/g, "/").replace(/\/\//g, "")
|
||||
} else {
|
||||
route = "/" + route.replace(".js", "").replace(/\\/g, "/")
|
||||
}
|
||||
const routeHandler = require(`./routes/${route}`)
|
||||
app.use(route, routeHandler)
|
||||
logger.log(`Route ${route.cyan.bold} registered`)
|
||||
}
|
||||
}
|
||||
|
||||
app.listen(config.web.port, () => {
|
||||
logger.log(`Server listening at port : ${config.web.port}`)
|
||||
})
|
||||
3
test.js
Normal file
3
test.js
Normal file
@ -0,0 +1,3 @@
|
||||
const x = require("./controllers/db")
|
||||
|
||||
console.log(x.getBan("a"))
|
||||
11
views/error.ejs
Normal file
11
views/error.ejs
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user