const express = require('express'); const os = require('os'); const process = require('process'); const geoip = require('geoip-lite'); const app = express(); const port = 3000; // Set JSON response indentation to 4 spaces app.set('json spaces', 4); // Function to convert bytes to binary MiB without decimals const bytesToBinaryMiB = (bytes) => Math.floor(bytes / (1024 * 1024)) + 'MiB'; // Function to convert uptime in seconds to "days hours minutes seconds" const secondsToDhms = (seconds) => { seconds = Number(seconds); const d = Math.floor(seconds / (3600 * 24)); const h = Math.floor((seconds % (3600 * 24)) / 3600); const m = Math.floor((seconds % 3600) / 60); const s = Math.floor(seconds % 60); return `${d} days ${h} hours ${m} minutes ${s} seconds`; }; // Root endpoint app.get('/', (req, res) => { const runtime = secondsToDhms(process.uptime()); res.setHeader('Content-Type', 'application/json'); res.status(200).send(JSON.stringify({ status: 200, message: `Server is running, uptime: ${runtime}` }, null, 4)); // JSON response with 4 spaces indentation }); // Specs endpoint app.get('/specs', (req, res) => { const networkInterfaces = os.networkInterfaces(); const specs = { system: { platform: os.platform(), release: os.release(), arch: os.arch(), uptime: secondsToDhms(os.uptime()), loadavg: os.loadavg() }, memory: { totalmem: bytesToBinaryMiB(os.totalmem()), freemem: bytesToBinaryMiB(os.freemem()) }, cpu: { cpus: os.cpus() }, network: { networkInterfaces: networkInterfaces, ipv4Info: {} }, user: { homedir: os.homedir(), hostname: os.hostname(), userInfo: os.userInfo() }, process: { pid: process.pid, version: process.version, title: process.title, argv: process.argv, memoryUsage: process.memoryUsage(), cwd: process.cwd(), execPath: process.execPath, platform: process.platform, uptime: secondsToDhms(process.uptime()), env: process.env } }; // Extract IPv4 info for each network interface Object.keys(networkInterfaces).forEach((key) => { specs.network.ipv4Info[key] = networkInterfaces[key] .filter((iface) => iface.family === 'IPv4') .map((iface) => { const geo = geoip.lookup(iface.address); return { address: iface.address, netmask: iface.netmask, mac: iface.mac, internal: iface.internal, country: geo ? geo.country : null, region: geo ? geo.region : null, city: geo ? geo.city : null, ll: geo ? geo.ll : null }; }); }); res.setHeader('Content-Type', 'application/json'); res.send(specs); }); // Start the server app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });