randydev commited on
Commit
7bf9990
·
verified ·
1 Parent(s): 245a10c

Upload federations.js

Browse files
Files changed (1) hide show
  1. plugins/federations.js +397 -112
plugins/federations.js CHANGED
@@ -1,9 +1,21 @@
1
  import express from 'express';
2
  import axios from 'axios';
 
 
3
  import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
4
  import { Federation } from '../models.js';
5
  const FedsRoutes = express.Router();
6
 
 
 
 
 
 
 
 
 
 
 
7
  /**
8
  * @swagger
9
  * /api/v2/federation/newfed:
@@ -65,20 +77,51 @@ const FedsRoutes = express.Router();
65
  * description: Internal server error.
66
  */
67
  FedsRoutes.post("/api/v2/federation/newfed", authenticateApiKey, apiLimiter, async (req, res) => {
68
- try {
69
- const { name, owner } = req.body;
70
-
71
- const existing = await Federation.findOne({ name });
72
- if (existing) return res.status(200).json({ error: "Federation already exists" });
73
-
74
- const federation = new Federation({ name, owner, banned_users: [], sub_federations: [] });
75
- await federation.save();
76
-
77
- res.json({ message: "Federation created successfully", federation });
78
- } catch (err) {
79
- console.log(err.message);
80
- res.status(500).json({ error: err.message });
81
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  });
83
 
84
  /**
@@ -127,23 +170,55 @@ FedsRoutes.post("/api/v2/federation/newfed", authenticateApiKey, apiLimiter, asy
127
  * description: Internal server error.
128
  */
129
  FedsRoutes.post("/api/v2/federation/subfed", authenticateApiKey, apiLimiter, async (req, res) => {
130
- try {
131
- const { parent_uuid, child_uuid } = req.body;
 
132
 
133
- const parent = await Federation.findOne({ uuid: parent_uuid });
134
- const child = await Federation.findOne({ uuid: child_uuid });
 
135
 
136
- if (!parent || !child) return res.status(404).json({ error: "Federation not found" });
137
 
138
- if (!parent.sub_federations.includes(child.uuid)) {
139
- parent.sub_federations.push(child.uuid);
140
- await parent.save();
141
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
- res.json({ message: `Federation ${child.name} is now a sub-federation of ${parent.name}` });
144
- } catch (err) {
145
- res.status(500).json({ error: err.message });
146
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  });
148
 
149
  /**
@@ -170,17 +245,48 @@ FedsRoutes.post("/api/v2/federation/subfed", authenticateApiKey, apiLimiter, asy
170
  * description: Returns
171
  */
172
  FedsRoutes.get("/api/v2/federation/getfed/:uuid", authenticateApiKey, apiLimiter, async (req, res) => {
173
- try {
174
- const federation = await Federation.findOne({ uuid: req.params.uuid });
175
-
176
- if (!federation) return res.status(404).json({ error: "Federation not found" });
 
 
 
177
 
178
- const subFeds = await Federation.find({ uuid: { $in: federation.sub_federations } });
179
 
180
- res.json({ federation, sub_federations: subFeds });
181
- } catch (err) {
182
- res.status(500).json({ error: err.message });
183
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  });
185
 
186
  /**
@@ -235,6 +341,32 @@ FedsRoutes.get("/api/v2/federation/getfed/:uuid", authenticateApiKey, apiLimiter
235
  */
236
  FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res) => {
237
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  const { name, user_id } = req.body;
239
 
240
  if (!name || !user_id || isNaN(user_id)) {
@@ -254,7 +386,12 @@ FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res)
254
  { uuid: { $in: federation.sub_federations } },
255
  { $pull: { banned_users: Number(user_id) } }
256
  );
257
-
 
 
 
 
 
258
  res.json({ success: true, message: `User ${user_id} unbanned from ${name} and its sub-federations.` });
259
  } catch (error) {
260
  res.status(500).json({ error: `Failed to unban user: ${error.message}` });
@@ -308,6 +445,32 @@ FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res)
308
  */
309
  FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async (req, res) => {
310
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
  const { federation_uuid, user_id } = req.body;
312
 
313
  if (!federation_uuid || !user_id || isNaN(user_id)) {
@@ -327,7 +490,12 @@ FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async
327
  { uuid: { $in: federation.sub_federations } },
328
  { $addToSet: { banned_users: Number(user_id) } }
329
  );
330
-
 
 
 
 
 
331
  res.json({ message: `User ${user_id} banned in ${federation.name} and all its sub-federations.` });
332
  console.log(user_id);
333
  } catch (err) {
@@ -389,30 +557,60 @@ FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async
389
  * description: Internal server error.
390
  */
391
  FedsRoutes.post("/api/v2/federation/ban-check", authenticateApiKey, async (req, res) => {
392
- try {
393
- const { federation_uuid, user_id } = req.body;
 
394
 
395
- if (!federation_uuid || !user_id || isNaN(user_id)) {
396
- return res.status(400).json({ error: "Federation UUID and valid user ID required." });
397
- }
398
 
399
- const federation = await Federation.findOne({ uuid: federation_uuid });
400
 
401
- if (!federation) {
402
- return res.status(404).json({ error: "Federation not found." });
403
- }
 
 
 
 
404
 
405
- const isBanned = federation.banned_users.includes(user_id);
406
 
407
- res.json({
408
- federation: federation.name,
409
- user_id,
410
- is_banned: isBanned
411
- });
412
 
413
- } catch (error) {
414
- res.status(500).json({ error: `Failed to check ban status: ${error.message}` });
415
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  });
417
 
418
  /**
@@ -469,30 +667,58 @@ FedsRoutes.post("/api/v2/federation/ban-check", authenticateApiKey, async (req,
469
  * description: Internal server error.
470
  */
471
  FedsRoutes.get("/api/v2/federation/fedstats", authenticateApiKey, async (req, res) => {
472
- try {
473
- const { uuid } = req.query;
 
474
 
475
- if (!uuid) {
476
- return res.status(400).json({ error: "Federation UUID is required" });
477
- }
478
 
479
- const federation = await Federation.findOne({ uuid });
480
 
481
- if (!federation) {
482
- return res.status(404).json({ error: "Federation not found" });
483
- }
 
 
 
 
 
 
 
 
484
 
485
- const stats = {
486
- federation_name: federation.name,
487
- total_banned_users: federation.banned_users.length,
488
- total_sub_federations: federation.sub_federations.length,
489
- owner: federation.owner,
490
- };
491
-
492
- res.json({ success: true, stats });
493
- } catch (error) {
494
- res.status(500).json({ error: `Failed to fetch federation stats: ${error.message}` });
495
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
  });
497
 
498
  /**
@@ -543,27 +769,57 @@ FedsRoutes.get("/api/v2/federation/fedstats", authenticateApiKey, async (req, re
543
  * description: Internal server error.
544
  */
545
  FedsRoutes.post("/api/v2/federation/unsubfed", authenticateApiKey, apiLimiter, async (req, res) => {
546
- try {
547
- const { parent_uuid, child_uuid } = req.body;
 
548
 
549
- if (!parent_uuid || !child_uuid) {
550
- return res.status(400).json({ error: "Both parent and child federation UUIDs are required." });
551
- }
552
 
553
- const parent = await Federation.findOne({ uuid: parent_uuid });
554
- const child = await Federation.findOne({ uuid: child_uuid });
555
 
556
- if (!parent || !child) {
557
- return res.status(404).json({ error: "Federation not found." });
558
- }
 
 
 
 
 
 
559
 
560
- parent.sub_federations = parent.sub_federations.filter(uuid => uuid !== child_uuid);
561
- await parent.save();
562
 
563
- res.json({ message: `Federation ${child.name} has been unsubscribed from ${parent.name}` });
564
- } catch (error) {
565
- res.status(500).json({ error: `Failed to unsubscribe sub-federation: ${error.message}` });
566
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567
  });
568
 
569
  /**
@@ -612,31 +868,60 @@ FedsRoutes.post("/api/v2/federation/unsubfed", authenticateApiKey, apiLimiter, a
612
  * description: Internal server error.
613
  */
614
  FedsRoutes.post("/api/v2/federation/renamefed", authenticateApiKey, apiLimiter, async (req, res) => {
615
- try {
616
- const { federation_uuid, new_name } = req.body;
 
617
 
618
- if (!federation_uuid || !new_name) {
619
- return res.status(400).json({ error: "Federation UUID and new name are required." });
620
- }
621
 
622
- const federation = await Federation.findOne({ uuid: federation_uuid });
623
 
624
- if (!federation) {
625
- return res.status(404).json({ error: "Federation not found." });
626
- }
 
 
 
 
627
 
628
- const existingFederation = await Federation.findOne({ name: new_name });
629
- if (existingFederation) {
630
- return res.status(400).json({ error: "Federation name already exists." });
631
- }
632
 
633
- federation.name = new_name;
634
- await federation.save();
635
 
636
- res.json({ message: `Federation renamed to ${new_name} successfully.` });
637
- } catch (error) {
638
- res.status(500).json({ error: `Failed to rename federation: ${error.message}` });
639
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
640
  });
641
 
642
- export { FedsRoutes };
 
1
  import express from 'express';
2
  import axios from 'axios';
3
+ import { TelegramUseLog } from '../lib/all.js';
4
+ import { Database } from '../database/database.js';
5
  import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
6
  import { Federation } from '../models.js';
7
  const FedsRoutes = express.Router();
8
 
9
+ const dbClient = new Database("AkenoXJs");
10
+ const db = dbClient.collection("api_keys");
11
+
12
+ async function updateIP(apiKey, newIP) {
13
+ await db.updateOne(
14
+ { key: apiKey },
15
+ { $addToSet: { ip_addresses: newIP } }
16
+ );
17
+ }
18
+
19
  /**
20
  * @swagger
21
  * /api/v2/federation/newfed:
 
77
  * description: Internal server error.
78
  */
79
  FedsRoutes.post("/api/v2/federation/newfed", authenticateApiKey, apiLimiter, async (req, res) => {
80
+ try {
81
+ const apiKey = req.headers['x-api-key'];
82
+ const userAgent = req.headers['user-agent'];
83
+
84
+ const xForwardedFor = req.headers['x-forwarded-for'];
85
+ const xRealIP = req.headers['x-real-ip'];
86
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
87
+
88
+ let realIP = req.ip;
89
+
90
+ if (xForwardedFor) {
91
+ realIP = xForwardedFor.split(',')[0].trim();
92
+ } else if (xRealIP) {
93
+ realIP = xRealIP;
94
+ } else if (cfConnectingIP) {
95
+ realIP = cfConnectingIP;
96
+ }
97
+
98
+ req.realIP = realIP;
99
+
100
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
101
+
102
+ const keyDoc = await db.findOne({ key: apiKey });
103
+
104
+ if (!keyDoc) {
105
+ return res.status(403).json({ error: "Invalid API Key." });
106
+ }
107
+ const { name, owner } = req.body;
108
+ const existing = await Federation.findOne({ name });
109
+ if (existing) return res.status(200).json({ error: "Federation already exists" });
110
+
111
+ await updateIP(apiKey, realIP);
112
+ await TelegramUseLog(
113
+ keyDoc.owner,
114
+ keyDoc.key,
115
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
116
+ );
117
+ const federation = new Federation({ name, owner, banned_users: [], sub_federations: [] });
118
+ await federation.save();
119
+
120
+ res.json({ message: "Federation created successfully", federation });
121
+ } catch (err) {
122
+ console.log(err.message);
123
+ res.status(500).json({ error: err.message });
124
+ }
125
  });
126
 
127
  /**
 
170
  * description: Internal server error.
171
  */
172
  FedsRoutes.post("/api/v2/federation/subfed", authenticateApiKey, apiLimiter, async (req, res) => {
173
+ try {
174
+ const apiKey = req.headers['x-api-key'];
175
+ const userAgent = req.headers['user-agent'];
176
 
177
+ const xForwardedFor = req.headers['x-forwarded-for'];
178
+ const xRealIP = req.headers['x-real-ip'];
179
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
180
 
181
+ let realIP = req.ip;
182
 
183
+ if (xForwardedFor) {
184
+ realIP = xForwardedFor.split(',')[0].trim();
185
+ } else if (xRealIP) {
186
+ realIP = xRealIP;
187
+ } else if (cfConnectingIP) {
188
+ realIP = cfConnectingIP;
189
+ }
190
+
191
+ req.realIP = realIP;
192
+
193
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
194
+
195
+ const keyDoc = await db.findOne({ key: apiKey });
196
+
197
+ if (!keyDoc) {
198
+ return res.status(403).json({ error: "Invalid API Key." });
199
+ }
200
 
201
+ const { parent_uuid, child_uuid } = req.body;
202
+
203
+ const parent = await Federation.findOne({ uuid: parent_uuid });
204
+ const child = await Federation.findOne({ uuid: child_uuid });
205
+
206
+ if (!parent || !child) return res.status(404).json({ error: "Federation not found" });
207
+
208
+ if (!parent.sub_federations.includes(child.uuid)) {
209
+ parent.sub_federations.push(child.uuid);
210
+ await parent.save();
211
+ }
212
+ await updateIP(apiKey, realIP);
213
+ await TelegramUseLog(
214
+ keyDoc.owner,
215
+ keyDoc.key,
216
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
217
+ );
218
+ res.json({ message: `Federation ${child.name} is now a sub-federation of ${parent.name}` });
219
+ } catch (err) {
220
+ res.status(500).json({ error: err.message });
221
+ }
222
  });
223
 
224
  /**
 
245
  * description: Returns
246
  */
247
  FedsRoutes.get("/api/v2/federation/getfed/:uuid", authenticateApiKey, apiLimiter, async (req, res) => {
248
+ try {
249
+ const apiKey = req.headers['x-api-key'];
250
+ const userAgent = req.headers['user-agent'];
251
+
252
+ const xForwardedFor = req.headers['x-forwarded-for'];
253
+ const xRealIP = req.headers['x-real-ip'];
254
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
255
 
256
+ let realIP = req.ip;
257
 
258
+ if (xForwardedFor) {
259
+ realIP = xForwardedFor.split(',')[0].trim();
260
+ } else if (xRealIP) {
261
+ realIP = xRealIP;
262
+ } else if (cfConnectingIP) {
263
+ realIP = cfConnectingIP;
264
+ }
265
+
266
+ req.realIP = realIP;
267
+
268
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
269
+
270
+ const keyDoc = await db.findOne({ key: apiKey });
271
+
272
+ if (!keyDoc) {
273
+ return res.status(403).json({ error: "Invalid API Key." });
274
+ }
275
+ const federation = await Federation.findOne({ uuid: req.params.uuid });
276
+
277
+ if (!federation) return res.status(404).json({ error: "Federation not found" });
278
+
279
+ const subFeds = await Federation.find({ uuid: { $in: federation.sub_federations } });
280
+ await updateIP(apiKey, realIP);
281
+ await TelegramUseLog(
282
+ keyDoc.owner,
283
+ keyDoc.key,
284
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
285
+ );
286
+ res.json({ federation, sub_federations: subFeds });
287
+ } catch (err) {
288
+ res.status(500).json({ error: err.message });
289
+ }
290
  });
291
 
292
  /**
 
341
  */
342
  FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res) => {
343
  try {
344
+ const apiKey = req.headers['x-api-key'];
345
+ const userAgent = req.headers['user-agent'];
346
+
347
+ const xForwardedFor = req.headers['x-forwarded-for'];
348
+ const xRealIP = req.headers['x-real-ip'];
349
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
350
+
351
+ let realIP = req.ip;
352
+
353
+ if (xForwardedFor) {
354
+ realIP = xForwardedFor.split(',')[0].trim();
355
+ } else if (xRealIP) {
356
+ realIP = xRealIP;
357
+ } else if (cfConnectingIP) {
358
+ realIP = cfConnectingIP;
359
+ }
360
+
361
+ req.realIP = realIP;
362
+
363
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
364
+
365
+ const keyDoc = await db.findOne({ key: apiKey });
366
+
367
+ if (!keyDoc) {
368
+ return res.status(403).json({ error: "Invalid API Key." });
369
+ }
370
  const { name, user_id } = req.body;
371
 
372
  if (!name || !user_id || isNaN(user_id)) {
 
386
  { uuid: { $in: federation.sub_federations } },
387
  { $pull: { banned_users: Number(user_id) } }
388
  );
389
+ await updateIP(apiKey, realIP);
390
+ await TelegramUseLog(
391
+ keyDoc.owner,
392
+ keyDoc.key,
393
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
394
+ );
395
  res.json({ success: true, message: `User ${user_id} unbanned from ${name} and its sub-federations.` });
396
  } catch (error) {
397
  res.status(500).json({ error: `Failed to unban user: ${error.message}` });
 
445
  */
446
  FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async (req, res) => {
447
  try {
448
+ const apiKey = req.headers['x-api-key'];
449
+ const userAgent = req.headers['user-agent'];
450
+
451
+ const xForwardedFor = req.headers['x-forwarded-for'];
452
+ const xRealIP = req.headers['x-real-ip'];
453
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
454
+
455
+ let realIP = req.ip;
456
+
457
+ if (xForwardedFor) {
458
+ realIP = xForwardedFor.split(',')[0].trim();
459
+ } else if (xRealIP) {
460
+ realIP = xRealIP;
461
+ } else if (cfConnectingIP) {
462
+ realIP = cfConnectingIP;
463
+ }
464
+
465
+ req.realIP = realIP;
466
+
467
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
468
+
469
+ const keyDoc = await db.findOne({ key: apiKey });
470
+
471
+ if (!keyDoc) {
472
+ return res.status(403).json({ error: "Invalid API Key." });
473
+ }
474
  const { federation_uuid, user_id } = req.body;
475
 
476
  if (!federation_uuid || !user_id || isNaN(user_id)) {
 
490
  { uuid: { $in: federation.sub_federations } },
491
  { $addToSet: { banned_users: Number(user_id) } }
492
  );
493
+ await updateIP(apiKey, realIP);
494
+ await TelegramUseLog(
495
+ keyDoc.owner,
496
+ keyDoc.key,
497
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
498
+ );
499
  res.json({ message: `User ${user_id} banned in ${federation.name} and all its sub-federations.` });
500
  console.log(user_id);
501
  } catch (err) {
 
557
  * description: Internal server error.
558
  */
559
  FedsRoutes.post("/api/v2/federation/ban-check", authenticateApiKey, async (req, res) => {
560
+ try {
561
+ const apiKey = req.headers['x-api-key'];
562
+ const userAgent = req.headers['user-agent'];
563
 
564
+ const xForwardedFor = req.headers['x-forwarded-for'];
565
+ const xRealIP = req.headers['x-real-ip'];
566
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
567
 
568
+ let realIP = req.ip;
569
 
570
+ if (xForwardedFor) {
571
+ realIP = xForwardedFor.split(',')[0].trim();
572
+ } else if (xRealIP) {
573
+ realIP = xRealIP;
574
+ } else if (cfConnectingIP) {
575
+ realIP = cfConnectingIP;
576
+ }
577
 
578
+ req.realIP = realIP;
579
 
580
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
 
 
 
 
581
 
582
+ const keyDoc = await db.findOne({ key: apiKey });
583
+
584
+ if (!keyDoc) {
585
+ return res.status(403).json({ error: "Invalid API Key." });
586
+ }
587
+ const { federation_uuid, user_id } = req.body;
588
+
589
+ if (!federation_uuid || !user_id || isNaN(user_id)) {
590
+ return res.status(400).json({ error: "Federation UUID and valid user ID required." });
591
+ }
592
+
593
+ const federation = await Federation.findOne({ uuid: federation_uuid });
594
+
595
+ if (!federation) {
596
+ return res.status(404).json({ error: "Federation not found." });
597
+ }
598
+
599
+ const isBanned = federation.banned_users.includes(user_id);
600
+ await updateIP(apiKey, realIP);
601
+ await TelegramUseLog(
602
+ keyDoc.owner,
603
+ keyDoc.key,
604
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
605
+ );
606
+ res.json({
607
+ federation: federation.name,
608
+ user_id,
609
+ is_banned: isBanned
610
+ });
611
+ } catch (error) {
612
+ res.status(500).json({ error: `Failed to check ban status: ${error.message}` });
613
+ }
614
  });
615
 
616
  /**
 
667
  * description: Internal server error.
668
  */
669
  FedsRoutes.get("/api/v2/federation/fedstats", authenticateApiKey, async (req, res) => {
670
+ try {
671
+ const apiKey = req.headers['x-api-key'];
672
+ const userAgent = req.headers['user-agent'];
673
 
674
+ const xForwardedFor = req.headers['x-forwarded-for'];
675
+ const xRealIP = req.headers['x-real-ip'];
676
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
677
 
678
+ let realIP = req.ip;
679
 
680
+ if (xForwardedFor) {
681
+ realIP = xForwardedFor.split(',')[0].trim();
682
+ } else if (xRealIP) {
683
+ realIP = xRealIP;
684
+ } else if (cfConnectingIP) {
685
+ realIP = cfConnectingIP;
686
+ }
687
+
688
+ req.realIP = realIP;
689
+
690
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
691
 
692
+ const keyDoc = await db.findOne({ key: apiKey });
693
+
694
+ if (!keyDoc) {
695
+ return res.status(403).json({ error: "Invalid API Key." });
696
+ }
697
+ const { uuid } = req.query;
698
+
699
+ if (!uuid) {
700
+ return res.status(400).json({ error: "Federation UUID is required" });
701
+ }
702
+ const federation = await Federation.findOne({ uuid });
703
+ if (!federation) {
704
+ return res.status(404).json({ error: "Federation not found" });
705
+ }
706
+ const stats = {
707
+ federation_name: federation.name,
708
+ total_banned_users: federation.banned_users.length,
709
+ total_sub_federations: federation.sub_federations.length,
710
+ owner: federation.owner,
711
+ };
712
+ await updateIP(apiKey, realIP);
713
+ await TelegramUseLog(
714
+ keyDoc.owner,
715
+ keyDoc.key,
716
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
717
+ );
718
+ res.json({ success: true, stats });
719
+ } catch (error) {
720
+ res.status(500).json({ error: `Failed to fetch federation stats: ${error.message}` });
721
+ }
722
  });
723
 
724
  /**
 
769
  * description: Internal server error.
770
  */
771
  FedsRoutes.post("/api/v2/federation/unsubfed", authenticateApiKey, apiLimiter, async (req, res) => {
772
+ try {
773
+ const apiKey = req.headers['x-api-key'];
774
+ const userAgent = req.headers['user-agent'];
775
 
776
+ const xForwardedFor = req.headers['x-forwarded-for'];
777
+ const xRealIP = req.headers['x-real-ip'];
778
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
779
 
780
+ let realIP = req.ip;
 
781
 
782
+ if (xForwardedFor) {
783
+ realIP = xForwardedFor.split(',')[0].trim();
784
+ } else if (xRealIP) {
785
+ realIP = xRealIP;
786
+ } else if (cfConnectingIP) {
787
+ realIP = cfConnectingIP;
788
+ }
789
+
790
+ req.realIP = realIP;
791
 
792
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
 
793
 
794
+ const keyDoc = await db.findOne({ key: apiKey });
795
+
796
+ if (!keyDoc) {
797
+ return res.status(403).json({ error: "Invalid API Key." });
798
+ }
799
+ const { parent_uuid, child_uuid } = req.body;
800
+
801
+ if (!parent_uuid || !child_uuid) {
802
+ return res.status(400).json({ error: "Both parent and child federation UUIDs are required." });
803
+ }
804
+
805
+ const parent = await Federation.findOne({ uuid: parent_uuid });
806
+ const child = await Federation.findOne({ uuid: child_uuid });
807
+
808
+ if (!parent || !child) {
809
+ return res.status(404).json({ error: "Federation not found." });
810
+ }
811
+ parent.sub_federations = parent.sub_federations.filter(uuid => uuid !== child_uuid);
812
+ await parent.save();
813
+ await updateIP(apiKey, realIP);
814
+ await TelegramUseLog(
815
+ keyDoc.owner,
816
+ keyDoc.key,
817
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
818
+ );
819
+ res.json({ message: `Federation ${child.name} has been unsubscribed from ${parent.name}` });
820
+ } catch (error) {
821
+ res.status(500).json({ error: `Failed to unsubscribe sub-federation: ${error.message}` });
822
+ }
823
  });
824
 
825
  /**
 
868
  * description: Internal server error.
869
  */
870
  FedsRoutes.post("/api/v2/federation/renamefed", authenticateApiKey, apiLimiter, async (req, res) => {
871
+ try {
872
+ const apiKey = req.headers['x-api-key'];
873
+ const userAgent = req.headers['user-agent'];
874
 
875
+ const xForwardedFor = req.headers['x-forwarded-for'];
876
+ const xRealIP = req.headers['x-real-ip'];
877
+ const cfConnectingIP = req.headers['cf-connecting-ip'];
878
 
879
+ let realIP = req.ip;
880
 
881
+ if (xForwardedFor) {
882
+ realIP = xForwardedFor.split(',')[0].trim();
883
+ } else if (xRealIP) {
884
+ realIP = xRealIP;
885
+ } else if (cfConnectingIP) {
886
+ realIP = cfConnectingIP;
887
+ }
888
 
889
+ req.realIP = realIP;
890
+
891
+ console.log(`🔍 Captured IPs:`, { xForwardedFor, xRealIP, cfConnectingIP, realIP });
 
892
 
893
+ const keyDoc = await db.findOne({ key: apiKey });
 
894
 
895
+ if (!keyDoc) {
896
+ return res.status(403).json({ error: "Invalid API Key." });
897
+ }
898
+ const { federation_uuid, new_name } = req.body;
899
+
900
+ if (!federation_uuid || !new_name) {
901
+ return res.status(400).json({ error: "Federation UUID and new name are required." });
902
+ }
903
+ const federation = await Federation.findOne({ uuid: federation_uuid });
904
+ if (!federation) {
905
+ return res.status(404).json({ error: "Federation not found." });
906
+ }
907
+
908
+ const existingFederation = await Federation.findOne({ name: new_name });
909
+ if (existingFederation) {
910
+ return res.status(400).json({ error: "Federation name already exists." });
911
+ }
912
+
913
+ federation.name = new_name;
914
+ await federation.save();
915
+ await updateIP(apiKey, realIP);
916
+ await TelegramUseLog(
917
+ keyDoc.owner,
918
+ keyDoc.key,
919
+ `Accessed: ${req.path}\nRealIP: ${realIP}\nUser-Agent: ${userAgent}`
920
+ );
921
+ res.json({ message: `Federation renamed to ${new_name} successfully.` });
922
+ } catch (error) {
923
+ res.status(500).json({ error: `Failed to rename federation: ${error.message}` });
924
+ }
925
  });
926
 
927
+ export { FedsRoutes };