Spaces:
Running
Running
const express = require('express'); | |
const path = require('path'); | |
const cors = require('cors'); | |
const axios = require('axios'); | |
const os = require('os'); | |
const fs = require('fs'); | |
const PORT = 25565; | |
const app = express(); | |
const apirouter = require('./dashinfo/api.js'); | |
var __path = process.cwd(); | |
app.use('/css', express.static(path.join(__dirname, 'css'))); | |
app.use('/js', express.static(path.join(__dirname, 'js'))); | |
app.use('/lib', express.static(path.join(__dirname, 'lib'))); | |
app.use('/img', express.static(path.join(__dirname, 'img'))); | |
app.use('/api', apirouter); | |
app.get('/', (req, res) => { | |
res.sendFile(__path + '/index.html'); | |
}); | |
app.get('/ai', (req, res) => { | |
res.sendFile(__path + '/views/ai.html'); | |
}); | |
app.use(cors()); | |
////////////////////////////// | |
//////////////////////////////////////////// | |
const dataFilePath = path.join(__dirname, 'visitor_data.json'); | |
let { visitorCount, visitorToday, lastUpdateDate } = loadVisitorData(); | |
const allowedPaths = ['/ai/aa', '/api', '/tool']; | |
app.use((req, res, next) => { | |
if (allowedPaths.some(path => req.path.startsWith(path))) { | |
updateVisitorCounts(); | |
} | |
next(); | |
}); | |
let Clock; // Declare Clock as a global variable | |
// Function to update the Clock variable | |
function updateClock() { | |
var d = new Date(); | |
const hour = d.getHours(); | |
const min = d.getMinutes(); | |
const sec = d.getSeconds(); | |
Clock = `${hour}:${min}:${sec}`; | |
} | |
setInterval(() => { | |
updateClock(); | |
}, 1000); | |
app.get('/info', (req, res) => { | |
const ip = req.connection.remoteAddress || req.socket.remoteAddress; | |
const currentTime = new Date().toLocaleTimeString(); | |
const cleanIp = ip.includes('::ffff:') ? ip.replace('::ffff:', '') : ip; | |
res.json({ | |
ip: cleanIp, | |
current_time: Clock | |
}); | |
}); | |
app.get('/count', (req, res) => { | |
try { | |
res.json({ | |
visitor_count: visitorCount, | |
visitor_today: visitorToday | |
}); | |
} catch (error) { | |
console.error(error); | |
res.status(500).json({ error: 'Internal Server Error' }); | |
} | |
}); | |
app.get('/status', (req, res) => { | |
try { | |
const uptime = os.uptime(); | |
const runtime = formatUptime(uptime); | |
const memory = { | |
free: formatBytes(os.freemem()), | |
total: formatBytes(os.totalmem()) | |
}; | |
res.json({ | |
runtime: runtime, | |
memory: `${memory.free} / ${memory.total}` | |
}); | |
} catch (error) { | |
console.error(error); | |
res.status(500).json({ error: 'Internal Server Error' }); | |
} | |
}); | |
function updateVisitorCounts() { | |
const currentDate = new Date().toDateString(); | |
if (currentDate !== lastUpdateDate) { | |
visitorToday = 0; | |
lastUpdateDate = currentDate; | |
saveVisitorData(); | |
} | |
visitorCount++; | |
visitorToday++; | |
saveVisitorData(); | |
} | |
function loadVisitorData() { | |
try { | |
const data = fs.readFileSync(dataFilePath, 'utf8'); | |
return JSON.parse(data); | |
} catch (error) { | |
return { | |
visitorCount: 0, | |
visitorToday: 0, | |
lastUpdateDate: new Date().toDateString() | |
}; | |
} | |
} | |
function saveVisitorData() { | |
const data = { | |
visitorCount, | |
visitorToday, | |
lastUpdateDate | |
}; | |
fs.writeFileSync(dataFilePath, JSON.stringify(data), 'utf8'); | |
} | |
function formatBytes(bytes) { | |
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | |
if (bytes === 0) return '0 Byte'; | |
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); | |
return Math.round(100 * bytes / Math.pow(1024, i)) / 100 + ' ' + sizes[i]; | |
} | |
function formatUptime(uptime) { | |
const hours = Math.floor(uptime / 3600); | |
const minutes = Math.floor((uptime % 3600) / 60); | |
const seconds = Math.floor(uptime % 60); | |
return `${hours} hours, ${minutes} minutes, ${seconds} seconds`; | |
} | |
app.use((req, res) => { | |
res.status(404).sendFile(__path + '/404.html'); | |
}); | |
app.listen(PORT, () => { | |
console.log("Server running on port " + PORT); | |
}); | |
module.exports = app; | |