Mr-studio commited on
Commit
1bb3a05
·
verified ·
1 Parent(s): 4fed404

Upload server.js

Browse files
Files changed (1) hide show
  1. server.js +54 -3
server.js CHANGED
@@ -3,6 +3,7 @@ import cors from 'cors';
3
  import helmet from 'helmet';
4
  import rateLimit from 'express-rate-limit';
5
  import { createClient } from '@supabase/supabase-js';
 
6
 
7
  const app = express();
8
  const PORT = 7860;
@@ -26,10 +27,25 @@ const SUPABASE_KEY = process.env.SUPABASE_KEY;
26
  const MY_API_TOKEN = process.env.MY_API_TOKEN;
27
  const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  app.post('/api/verify', async (req, res) => {
30
  try {
31
  const { token, serial = "0", nick = "Unknown" } = req.body;
32
-
33
  console.log("Полученные данные:", req.body);
34
 
35
  if (!token) {
@@ -72,8 +88,14 @@ app.post('/api/verify', async (req, res) => {
72
  console.error("Ошибка обновления Supabase:", updateError);
73
  throw updateError;
74
  }
75
-
76
  console.log(`[NEW] Ключ ${cleanToken} активовано для серіалу: ${serial}`);
 
 
 
 
 
 
77
  } else {
78
  if (userKey.serial !== serial) {
79
  return res.status(403).json({
@@ -157,6 +179,12 @@ app.post('/api/appeal', async (req, res) => {
157
  return res.status(500).json({ status: false, message: "Ошибка сохранения обращения в базу данных" });
158
  }
159
 
 
 
 
 
 
 
160
  // Отправляем Lua-скрипту ответ, что все прошло отлично
161
  return res.status(200).json({
162
  status: true,
@@ -172,7 +200,7 @@ app.post('/api/appeal', async (req, res) => {
172
  app.post('/api/norma', async (req, res) => {
173
  try {
174
  const { token, date, online, report, z, mute, jail, warn, ban } = req.body;
175
-
176
  console.log(`[NORMA] Получены данные от токена: ${token ? token.substring(0, 5) + '...' : 'НЕТ'}`);
177
 
178
  if (!token) {
@@ -250,6 +278,29 @@ app.post('/api/norma', async (req, res) => {
250
  }
251
  });
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  app.listen(PORT, () => {
254
  console.log(`Сервер захисту запущено на порті ${PORT}`);
255
  });
 
3
  import helmet from 'helmet';
4
  import rateLimit from 'express-rate-limit';
5
  import { createClient } from '@supabase/supabase-js';
6
+ import os from 'os';
7
 
8
  const app = express();
9
  const PORT = 7860;
 
27
  const MY_API_TOKEN = process.env.MY_API_TOKEN;
28
  const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
29
 
30
+ async function sendSystemLog(title, content, type = 'system') {
31
+ try {
32
+ const { error } = await supabase.from('logs').insert([{
33
+ title: title,
34
+ content: content,
35
+ type: type,
36
+ admin: 'API Сервер',
37
+ created_at: new Date().toISOString()
38
+ }]);
39
+ if (error) console.error("Ошибка записи лога в БД:", error);
40
+ } catch (err) {
41
+ console.error("Критическая ошибка логирования:", err);
42
+ }
43
+ }
44
+
45
  app.post('/api/verify', async (req, res) => {
46
  try {
47
  const { token, serial = "0", nick = "Unknown" } = req.body;
48
+
49
  console.log("Полученные данные:", req.body);
50
 
51
  if (!token) {
 
88
  console.error("Ошибка обновления Supabase:", updateError);
89
  throw updateError;
90
  }
91
+
92
  console.log(`[NEW] Ключ ${cleanToken} активовано для серіалу: ${serial}`);
93
+
94
+ await sendSystemLog(
95
+ "Новая активация ключа",
96
+ `Ключ ${cleanToken.substring(0, 8)}... привязан к устройству ${serial}`,
97
+ "auth"
98
+ );
99
  } else {
100
  if (userKey.serial !== serial) {
101
  return res.status(403).json({
 
179
  return res.status(500).json({ status: false, message: "Ошибка сохранения обращения в базу данных" });
180
  }
181
 
182
+ await sendSystemLog(
183
+ "Поступил новый тикет",
184
+ `Тип: ${type || "—"} | Сервер: ${server || "—"} | Ключ: ${cleanToken.substring(0, 8)}...`,
185
+ "ticket"
186
+ );
187
+
188
  // Отправляем Lua-скрипту ответ, что все прошло отлично
189
  return res.status(200).json({
190
  status: true,
 
200
  app.post('/api/norma', async (req, res) => {
201
  try {
202
  const { token, date, online, report, z, mute, jail, warn, ban } = req.body;
203
+
204
  console.log(`[NORMA] Получены данные от токена: ${token ? token.substring(0, 5) + '...' : 'НЕТ'}`);
205
 
206
  if (!token) {
 
278
  }
279
  });
280
 
281
+ app.get('/api/status', (req, res) => {
282
+ try {
283
+ const totalMem = os.totalmem();
284
+ const freeMem = os.freemem();
285
+ const usedMem = totalMem - freeMem;
286
+ const ramPercent = Math.round((usedMem / totalMem) * 100);
287
+
288
+ const cpus = os.cpus().length;
289
+ const cpuLoad = Math.min(Math.round((os.loadavg()[0] / cpus) * 100), 100);
290
+
291
+ res.json({
292
+ success: true,
293
+ uptime: process.uptime(),
294
+ cpu: cpuLoad || 1,
295
+ ramUsed: Math.round(usedMem / 1024 / 1024),
296
+ ramTotal: Math.round(totalMem / 1024 / 1024),
297
+ ramPercent: ramPercent
298
+ });
299
+ } catch (err) {
300
+ res.status(500).json({ success: false });
301
+ }
302
+ });
303
+
304
  app.listen(PORT, () => {
305
  console.log(`Сервер захисту запущено на порті ${PORT}`);
306
  });