164 lines
5.9 KiB
JavaScript
164 lines
5.9 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const reasonFlags = global.reasonFlags
|
|
|
|
const { execSync } = require('child_process');
|
|
|
|
// Create a MariaDB connection pool
|
|
const pool = global.db_pool
|
|
|
|
const rateLimit = require('../rateLimit.js');
|
|
router.use(rateLimit.middleware);
|
|
|
|
router.get("/health", async (req,res) => {
|
|
try {
|
|
// Get a connection from the pool
|
|
const connection = await pool.getConnection();
|
|
|
|
try {
|
|
// Execute a simple query to check the connection
|
|
await connection.query('SELECT 1');
|
|
res.status(200).json({ status: 'OK' });
|
|
} finally {
|
|
// Release the connection back to the pool
|
|
connection.release();
|
|
}
|
|
} catch (err) {
|
|
console.error('Database connection error:', err);
|
|
res.status(500).json({ status: 'ERROR', error: 'Database connection error' });
|
|
}
|
|
});
|
|
|
|
router.get("/v1/myIP", (req,res) => {
|
|
res.json({
|
|
ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress
|
|
})
|
|
});
|
|
|
|
// Route to fetch all bans
|
|
router.get('/v1/bans', async (req, res) => {
|
|
try {
|
|
// Get a connection from the pool
|
|
const connection = await pool.getConnection();
|
|
|
|
try {
|
|
// Execute the query to fetch all rows from the `bans` table
|
|
const rows = await connection.query('SELECT * FROM bans');
|
|
// Convert all timestamps into epoch
|
|
rows.forEach(row => {
|
|
row.expiresTimestamp = row.expiresTimestamp ? new Date(`${row.expiresTimestamp}Z`).getTime() : null
|
|
row.banTimestamp = row.banTimestamp ? new Date(`${row.banTimestamp}`).getTime() : null
|
|
row.valid = row.expiresTimestamp ? new Date(row.expiresTimestamp) > new Date() : true
|
|
});
|
|
// Send the results as a JSON response
|
|
res.json(rows);
|
|
} finally {
|
|
// Release the connection back to the pool
|
|
connection.release();
|
|
}
|
|
} catch (err) {
|
|
console.error('Error fetching bans:', err);
|
|
|
|
// Respond with a 500 Internal Server Error status if something goes wrong
|
|
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
|
|
}
|
|
});
|
|
|
|
router.get("/v1/ban/roblox/:uid", async (req, res) => {
|
|
if (!req.params.uid) return res.status(400).json({error: "Specify user ID!"});
|
|
try {
|
|
// Get a connection from the pool
|
|
const connection = await pool.getConnection();
|
|
|
|
try {
|
|
// Execute the query to fetch all rows from the `bans` table
|
|
const rows = await connection.query('SELECT * FROM bans WHERE robloxId = ?', [req.params.uid]);
|
|
// Convert all timestamps into epoch
|
|
rows.forEach(row => {
|
|
row.expiresTimestamp = row.expiresTimestamp ? new Date(`${row.expiresTimestamp}Z`).getTime() : null
|
|
row.banTimestamp = row.banTimestamp ? new Date(`${row.banTimestamp}`).getTime() : null
|
|
row.valid = row.expiresTimestamp ? new Date(row.expiresTimestamp) > new Date() : true
|
|
});
|
|
// Send the results as a JSON response
|
|
res.json(rows);
|
|
} finally {
|
|
// Release the connection back to the pool
|
|
connection.release();
|
|
}
|
|
} catch (err) {
|
|
console.error('Error fetching bans:', err);
|
|
|
|
// Respond with a 500 Internal Server Error status if something goes wrong
|
|
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
|
|
}
|
|
})
|
|
|
|
router.get("/v1/ban/discord/:uid", async (req, res) => {
|
|
if (!req.params.uid) return res.status(400).json({error: "Specify user ID!"});
|
|
try {
|
|
// Get a connection from the pool
|
|
const connection = await pool.getConnection();
|
|
|
|
try {
|
|
// Execute the query to fetch all rows from the `bans` table
|
|
const rows = await connection.query('SELECT * FROM bans WHERE discordId = ?', [req.params.uid]);
|
|
// Convert all timestamps into epoch
|
|
rows.forEach(row => {
|
|
row.expiresTimestamp = row.expiresTimestamp ? new Date(`${row.expiresTimestamp}Z`).getTime() : null
|
|
row.banTimestamp = row.banTimestamp ? new Date(`${row.banTimestamp}`).getTime() : null
|
|
row.valid = row.expiresTimestamp ? new Date(row.expiresTimestamp) > new Date() : true
|
|
});
|
|
|
|
// Send the results as a JSON response
|
|
res.json(rows);
|
|
} finally {
|
|
// Release the connection back to the pool
|
|
connection.release();
|
|
}
|
|
} catch (err) {
|
|
console.error('Error fetching bans:', err);
|
|
|
|
// Respond with a 500 Internal Server Error status if something goes wrong
|
|
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
|
|
}
|
|
})
|
|
|
|
router.get("/v1/ban/id/:id", async (req, res) => {
|
|
if (!req.params.id) return res.status(400).json({error: "Specify ban ID!"});
|
|
try {
|
|
// Get a connection from the pool
|
|
const connection = await pool.getConnection();
|
|
|
|
try {
|
|
// Execute the query to fetch all rows from the `bans` table
|
|
const rows = await connection.query('SELECT * FROM bans WHERE id = ?', [req.params.id]);
|
|
// Convert all timestamps into epoch
|
|
rows.forEach(row => {
|
|
row.expiresTimestamp = row.expiresTimestamp ? new Date(`${row.expiresTimestamp}Z`).getTime() : null
|
|
row.banTimestamp = row.banTimestamp ? new Date(`${row.banTimestamp}`).getTime() : null
|
|
row.valid = row.expiresTimestamp ? new Date(row.expiresTimestamp) > new Date() : true
|
|
});
|
|
|
|
// Send the results as a JSON response
|
|
res.json(rows);
|
|
} finally {
|
|
// Release the connection back to the pool
|
|
connection.release();
|
|
}
|
|
} catch (err) {
|
|
console.error('Error fetching bans:', err);
|
|
|
|
// Respond with a 500 Internal Server Error status if something goes wrong
|
|
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
|
|
}
|
|
});
|
|
|
|
router.get("/v1/info", (req,res) => {
|
|
res.json({
|
|
commit_hash: execSync('git rev-parse HEAD').toString().trim(),
|
|
reasonFlags,
|
|
currentServerTime: new Date().getTime()
|
|
})
|
|
})
|
|
|
|
module.exports = router; |