File size: 3,846 Bytes
6b393b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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;