Note: We appreciate your feedback and bug reports to continue improving our platform. Thank you for your continued support!
151 lines
4.1 KiB
JavaScript
151 lines
4.1 KiB
JavaScript
const express = require('express');
|
|
const session = require('express-session');
|
|
const passport = require('passport');
|
|
const bodyParser = require('body-parser');
|
|
const { logger, logRequestInfo, ErrorLogger } = require('./config/logs');
|
|
const path = require("path");
|
|
const { version } = require('./package.json');
|
|
const axios = require('axios');
|
|
const flash = require('connect-flash');
|
|
const crypto = require('crypto');
|
|
const fs = require('fs');
|
|
const SystemReport = require('./models/reportManager.js');
|
|
const routes = require('./routes/routes.js');
|
|
const cron = require('node-cron');
|
|
const chalk = require('chalk');
|
|
|
|
require('dotenv').config();
|
|
const app = express();
|
|
|
|
require('./models/fileCreated.js');
|
|
|
|
let setup;
|
|
try {
|
|
setup = JSON.parse(fs.readFileSync(path.join(__dirname, 'data', 'setup.json'), 'utf8'));
|
|
} catch (err) {
|
|
console.error('Error reading setup.json:', err);
|
|
setup = {};
|
|
}
|
|
|
|
if (setup.discord !== undefined) {
|
|
require('./models/Passport-Discord.js');
|
|
}
|
|
|
|
if (setup.ldap !== undefined) {
|
|
require('./models/Passport-ActiveDirectory.js');
|
|
}
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
app.get(['/data/user.json', '/data/file_info.json', '/data/setup.json'], (req, res) => {
|
|
res.status(403).json({ error: 'Access Denied. You do not have permission to access this resource.' });
|
|
});
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
function generateSecretKey() {
|
|
return crypto.randomBytes(64).toString('hex');
|
|
}
|
|
|
|
app.use(session({
|
|
secret: generateSecretKey(),
|
|
resave: false,
|
|
saveUninitialized: true,
|
|
cookie: { secure: false }
|
|
}));
|
|
|
|
app.use(passport.initialize());
|
|
app.use(passport.session());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
app.use(bodyParser.json());
|
|
app.use(flash());
|
|
|
|
app.use('/public', express.static(path.join(__dirname, 'public')));
|
|
app.set('view engine', 'ejs');
|
|
app.set('views', __dirname + '/views');
|
|
app.use(routes);
|
|
|
|
app.use(logRequestInfo);
|
|
|
|
cron.schedule('00 03 * * *', async () => {
|
|
try {
|
|
const report = await SystemReport.generate();
|
|
if (report !== null) {
|
|
logger.info('System error report generated successfully');
|
|
}
|
|
} catch (err) {
|
|
ErrorLogger.error('Error generating report :', err);
|
|
}
|
|
});
|
|
|
|
cron.schedule('0 * * * *', async () => {
|
|
try {
|
|
const fileInfoData = await fs.promises.readFile(path.join(__dirname, '/data/', 'file_info.json'), 'utf8');
|
|
const fileInfo = JSON.parse(fileInfoData);
|
|
|
|
const now = new Date();
|
|
|
|
for (let index = fileInfo.length - 1; index >= 0; index--) {
|
|
const file = fileInfo[index];
|
|
const expiry = new Date(file.expiryDate);
|
|
|
|
if ((file.expiryDate && expiry <= now) || !(await fileExists(file.path))) {
|
|
fileInfo.splice(index, 1);
|
|
}
|
|
}
|
|
|
|
await fs.promises.writeFile(path.join(__dirname, 'file_info.json'), JSON.stringify(fileInfo, null, 2), 'utf8');
|
|
logger.info('Successfully checked file expirations and updated file_info.json');
|
|
} catch (err) {
|
|
ErrorLogger.error(`Failed to check file expirations: ${err}`);
|
|
}
|
|
});
|
|
|
|
async function fileExists(filePath) {
|
|
try {
|
|
await fs.promises.access(filePath);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function getAllFiles(dirPath, arrayOfFiles) {
|
|
const files = fs.readdirSync(dirPath);
|
|
|
|
arrayOfFiles = arrayOfFiles || [];
|
|
|
|
files.forEach(function(file) {
|
|
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
|
|
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
|
|
} else {
|
|
arrayOfFiles.push(path.join(dirPath, "/", file));
|
|
}
|
|
});
|
|
|
|
return arrayOfFiles;
|
|
}
|
|
|
|
const allFiles = getAllFiles(__dirname);
|
|
|
|
const SERVER = process.env.PORT || 5053;
|
|
app.listen(SERVER, () => {
|
|
SERVER.timeout = 300000
|
|
allFiles.forEach(file => {
|
|
|
|
console.log(`[ ${chalk.green('OK')} ] Loaded file: ${file}`);
|
|
});
|
|
|
|
console.clear();
|
|
if (logger) {
|
|
logger.info(`☀️ Welcome to the Content Delivery Network Server`);
|
|
logger.info(`🚀 Your server is available and running on port ${SERVER}`);
|
|
logger.info(`⚜️ Application developed by Dinawo, part of the Myaxrin Labs group`);
|
|
logger.info(`♨️ Version: ${version}`);
|
|
console.log('');
|
|
} else {
|
|
console.error('🔴 Logger is not initialized');
|
|
}
|
|
});
|