63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
const express = require('express');
|
|
|
|
const app = express();
|
|
const port = process.env.SERVER_PORT || 3000;
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const logFile = path.join(__dirname, process.env.LOG_FILE || 'server.log');
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.use((req, res, next) => {
|
|
//Logger
|
|
// [timestamp] method - url; UA; IP; roblox-id header if it exists
|
|
const timestamp = new Date().toISOString();
|
|
const method = req.method;
|
|
const url = req.originalUrl;
|
|
const userAgent = req.headers['user-agent'] || 'unknown';
|
|
const ip = req.headers['x-forwarded-for'] || req.remoteAddress || 'unknown';
|
|
const robloxId = req.headers['roblox-id'] || 'none';
|
|
const logEntry = `[${timestamp}] ${method} - ${url}; UA: ${userAgent}; IP: ${ip}; Roblox-ID: ${robloxId}\n`;
|
|
fs.appendFileSync(logFile, logEntry, 'utf8', (err) => {
|
|
if (err) {
|
|
console.error('Error writing to log file:', err);
|
|
}
|
|
});
|
|
if (userAgent.includes('Roblox')) {
|
|
console.log(logEntry.trim());
|
|
}
|
|
next();
|
|
});
|
|
|
|
app.get('/', (req, res) => {
|
|
res.send('Hello, world!');
|
|
});
|
|
|
|
app.get('/time.php', (req, res) => {
|
|
// This endpoint returns the current time in a specific format based on the timezone provided
|
|
const tz = req.query.zone || 'UTC';
|
|
const date = new Date();
|
|
try {
|
|
const options = { timeZone: tz, hour12: false };
|
|
const formatter = new Intl.DateTimeFormat('en-GB', {
|
|
...options,
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
second: '2-digit'
|
|
});
|
|
const parts = formatter.formatToParts(date);
|
|
const get = type => parts.find(p => p.type === type).value;
|
|
const formatted = `${get('day')}${get('month')}${get('year')}${get('hour')}${get('minute')}${get('second')}`;
|
|
res.send(formatted);
|
|
} catch (e) {
|
|
res.status(400).send('Invalid timezone');
|
|
}
|
|
});
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server is running on port ${port}`);
|
|
}); |