randydev commited on
Commit
4812def
·
verified ·
1 Parent(s): 216a7bf
Files changed (1) hide show
  1. plugins/federations.js +1 -291
plugins/federations.js CHANGED
@@ -78,42 +78,9 @@ async function updateIP(apiKey, newIP) {
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
 
@@ -171,33 +138,6 @@ FedsRoutes.post("/api/v2/federation/newfed", authenticateApiKey, apiLimiter, asy
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 });
@@ -209,12 +149,6 @@ FedsRoutes.post("/api/v2/federation/subfed", authenticateApiKey, apiLimiter, asy
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 });
@@ -246,43 +180,11 @@ FedsRoutes.post("/api/v2/federation/subfed", authenticateApiKey, apiLimiter, asy
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 });
@@ -341,32 +243,6 @@ FedsRoutes.get("/api/v2/federation/getfed/:uuid", authenticateApiKey, apiLimiter
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,12 +262,6 @@ FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res)
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,32 +315,6 @@ FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res)
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,12 +334,6 @@ FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async
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) {
@@ -558,32 +396,6 @@ FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async
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)) {
@@ -597,12 +409,6 @@ FedsRoutes.post("/api/v2/federation/ban-check", authenticateApiKey, async (req,
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,
@@ -668,32 +474,6 @@ FedsRoutes.post("/api/v2/federation/ban-check", authenticateApiKey, async (req,
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) {
@@ -709,12 +489,6 @@ FedsRoutes.get("/api/v2/federation/fedstats", authenticateApiKey, async (req, re
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}` });
@@ -770,32 +544,6 @@ FedsRoutes.get("/api/v2/federation/fedstats", authenticateApiKey, async (req, re
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) {
@@ -810,12 +558,6 @@ FedsRoutes.post("/api/v2/federation/unsubfed", authenticateApiKey, apiLimiter, a
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}` });
@@ -869,32 +611,6 @@ FedsRoutes.post("/api/v2/federation/unsubfed", authenticateApiKey, apiLimiter, a
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) {
@@ -912,16 +628,10 @@ FedsRoutes.post("/api/v2/federation/renamefed", authenticateApiKey, apiLimiter,
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 };
 
78
  */
79
  FedsRoutes.post("/api/v2/federation/newfed", authenticateApiKey, apiLimiter, async (req, res) => {
80
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  const { name, owner } = req.body;
82
  const existing = await Federation.findOne({ name });
83
  if (existing) return res.status(200).json({ error: "Federation already exists" });
 
 
 
 
 
 
 
84
  const federation = new Federation({ name, owner, banned_users: [], sub_federations: [] });
85
  await federation.save();
86
 
 
138
  */
139
  FedsRoutes.post("/api/v2/federation/subfed", authenticateApiKey, apiLimiter, async (req, res) => {
140
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  const { parent_uuid, child_uuid } = req.body;
142
 
143
  const parent = await Federation.findOne({ uuid: parent_uuid });
 
149
  parent.sub_federations.push(child.uuid);
150
  await parent.save();
151
  }
 
 
 
 
 
 
152
  res.json({ message: `Federation ${child.name} is now a sub-federation of ${parent.name}` });
153
  } catch (err) {
154
  res.status(500).json({ error: err.message });
 
180
  */
181
  FedsRoutes.get("/api/v2/federation/getfed/:uuid", authenticateApiKey, apiLimiter, async (req, res) => {
182
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  const federation = await Federation.findOne({ uuid: req.params.uuid });
184
 
185
  if (!federation) return res.status(404).json({ error: "Federation not found" });
186
 
187
  const subFeds = await Federation.find({ uuid: { $in: federation.sub_federations } });
 
 
 
 
 
 
188
  res.json({ federation, sub_federations: subFeds });
189
  } catch (err) {
190
  res.status(500).json({ error: err.message });
 
243
  */
244
  FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res) => {
245
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  const { name, user_id } = req.body;
247
 
248
  if (!name || !user_id || isNaN(user_id)) {
 
262
  { uuid: { $in: federation.sub_federations } },
263
  { $pull: { banned_users: Number(user_id) } }
264
  );
 
 
 
 
 
 
265
  res.json({ success: true, message: `User ${user_id} unbanned from ${name} and its sub-federations.` });
266
  } catch (error) {
267
  res.status(500).json({ error: `Failed to unban user: ${error.message}` });
 
315
  */
316
  FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async (req, res) => {
317
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  const { federation_uuid, user_id } = req.body;
319
 
320
  if (!federation_uuid || !user_id || isNaN(user_id)) {
 
334
  { uuid: { $in: federation.sub_federations } },
335
  { $addToSet: { banned_users: Number(user_id) } }
336
  );
 
 
 
 
 
 
337
  res.json({ message: `User ${user_id} banned in ${federation.name} and all its sub-federations.` });
338
  console.log(user_id);
339
  } catch (err) {
 
396
  */
397
  FedsRoutes.post("/api/v2/federation/ban-check", authenticateApiKey, async (req, res) => {
398
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  const { federation_uuid, user_id } = req.body;
400
 
401
  if (!federation_uuid || !user_id || isNaN(user_id)) {
 
409
  }
410
 
411
  const isBanned = federation.banned_users.includes(user_id);
 
 
 
 
 
 
412
  res.json({
413
  federation: federation.name,
414
  user_id,
 
474
  */
475
  FedsRoutes.get("/api/v2/federation/fedstats", authenticateApiKey, async (req, res) => {
476
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
477
  const { uuid } = req.query;
478
 
479
  if (!uuid) {
 
489
  total_sub_federations: federation.sub_federations.length,
490
  owner: federation.owner,
491
  };
 
 
 
 
 
 
492
  res.json({ success: true, stats });
493
  } catch (error) {
494
  res.status(500).json({ error: `Failed to fetch federation stats: ${error.message}` });
 
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) {
 
558
  }
559
  parent.sub_federations = parent.sub_federations.filter(uuid => uuid !== child_uuid);
560
  await parent.save();
 
 
 
 
 
 
561
  res.json({ message: `Federation ${child.name} has been unsubscribed from ${parent.name}` });
562
  } catch (error) {
563
  res.status(500).json({ error: `Failed to unsubscribe sub-federation: ${error.message}` });
 
611
  */
612
  FedsRoutes.post("/api/v2/federation/renamefed", authenticateApiKey, apiLimiter, async (req, res) => {
613
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
614
  const { federation_uuid, new_name } = req.body;
615
 
616
  if (!federation_uuid || !new_name) {
 
628
 
629
  federation.name = new_name;
630
  await federation.save();
 
 
 
 
 
 
631
  res.json({ message: `Federation renamed to ${new_name} successfully.` });
632
  } catch (error) {
633
  res.status(500).json({ error: `Failed to rename federation: ${error.message}` });
634
  }
635
  });
636
 
637
+ export { FedsRoutes };