- Remplacer fallback SERVER_DIR de '/home/innotex/Documents/...' à '/mc-server' - Remplacer RCON_HOST de 'localhost' à '172.17.0.1' (Docker bridge gateway) - 8 fichiers corrigés: server.js + 7 routes - Localhost n'est pas accessible depuis Docker vers l'host - 172.17.0.1 est la gateway Docker qui permet au container d'accéder l'host
139 lines
3.6 KiB
JavaScript
139 lines
3.6 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs-extra');
|
|
const path = require('path');
|
|
|
|
const router = express.Router();
|
|
const SERVER_DIR = process.env.SERVER_DIR || '/mc-server';
|
|
|
|
function isAuthenticated(req, res, next) {
|
|
if (req.session.user) {
|
|
next();
|
|
} else {
|
|
res.status(401).json({ error: 'Non authentifié' });
|
|
}
|
|
}
|
|
|
|
router.get('/', isAuthenticated, async (req, res) => {
|
|
try {
|
|
const { lines = 100 } = req.query;
|
|
const logsDir = SERVER_DIR;
|
|
|
|
let logFile = path.join(logsDir, 'latest.log');
|
|
|
|
if (!await fs.pathExists(logFile)) {
|
|
logFile = path.join(logsDir, 'ForgeModLoader-server-0.log');
|
|
}
|
|
|
|
if (!await fs.pathExists(logFile)) {
|
|
return res.json({ logs: [], message: 'Aucun fichier log trouvé' });
|
|
}
|
|
|
|
const content = await fs.readFile(logFile, 'utf-8');
|
|
const logLines = content.split('\n');
|
|
const lastLines = logLines.slice(-parseInt(lines));
|
|
|
|
res.json({
|
|
logs: lastLines,
|
|
file: path.basename(logFile),
|
|
totalLines: logLines.length
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Erreur logs:', error);
|
|
res.status(500).json({ error: 'Erreur serveur' });
|
|
}
|
|
});
|
|
|
|
router.get('/files', isAuthenticated, async (req, res) => {
|
|
try {
|
|
const logsDir = SERVER_DIR;
|
|
const files = await fs.readdir(logsDir);
|
|
|
|
const logFiles = files.filter(f =>
|
|
f.includes('log') || f.includes('Log')
|
|
);
|
|
|
|
const filesWithStats = await Promise.all(
|
|
logFiles.map(async (f) => {
|
|
const stats = await fs.stat(path.join(logsDir, f));
|
|
return {
|
|
name: f,
|
|
size: (stats.size / 1024).toFixed(2) + ' KB',
|
|
modified: stats.mtime
|
|
};
|
|
})
|
|
);
|
|
|
|
res.json(filesWithStats.sort((a, b) => new Date(b.modified) - new Date(a.modified)));
|
|
|
|
} catch (error) {
|
|
console.error('Erreur liste logs:', error);
|
|
res.status(500).json({ error: 'Erreur serveur' });
|
|
}
|
|
});
|
|
|
|
router.get('/file/:filename', isAuthenticated, async (req, res) => {
|
|
try {
|
|
const { filename } = req.params;
|
|
const { lines = 100 } = req.query;
|
|
|
|
if (filename.includes('..')) {
|
|
return res.status(400).json({ error: 'Accès non autorisé' });
|
|
}
|
|
|
|
const filePath = path.join(SERVER_DIR, filename);
|
|
|
|
if (!await fs.pathExists(filePath)) {
|
|
return res.status(404).json({ error: 'Fichier non trouvé' });
|
|
}
|
|
|
|
const content = await fs.readFile(filePath, 'utf-8');
|
|
const fileLines = content.split('\n');
|
|
const lastLines = fileLines.slice(-parseInt(lines));
|
|
|
|
res.json({
|
|
logs: lastLines,
|
|
file: filename,
|
|
totalLines: fileLines.length
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Erreur lecture log:', error);
|
|
res.status(500).json({ error: 'Erreur serveur' });
|
|
}
|
|
});
|
|
|
|
router.get('/search', isAuthenticated, async (req, res) => {
|
|
try {
|
|
const { query } = req.query;
|
|
|
|
if (!query || query.length < 2) {
|
|
return res.status(400).json({ error: 'Requête trop courte' });
|
|
}
|
|
|
|
let logFile = path.join(SERVER_DIR, 'latest.log');
|
|
if (!await fs.pathExists(logFile)) {
|
|
logFile = path.join(SERVER_DIR, 'ForgeModLoader-server-0.log');
|
|
}
|
|
|
|
if (!await fs.pathExists(logFile)) {
|
|
return res.json({ results: [] });
|
|
}
|
|
|
|
const content = await fs.readFile(logFile, 'utf-8');
|
|
const lines = content.split('\n');
|
|
const results = lines
|
|
.map((line, index) => ({ line, index }))
|
|
.filter(({ line }) => line.toLowerCase().includes(query.toLowerCase()))
|
|
.slice(-50);
|
|
|
|
res.json({ results });
|
|
|
|
} catch (error) {
|
|
console.error('Erreur recherche:', error);
|
|
res.status(500).json({ error: 'Erreur serveur' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|