File size: 22,394 Bytes
70335fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f115e7
70335fc
cc43160
1c2f245
1002d9f
f836f2f
ff5dd77
 
7864014
7da0f43
2a1da11
0073410
a655a6b
ff5dd77
9d7429f
 
 
7864014
9d7429f
 
ea9ab95
8e9d6ef
4acf951
ea9ab95
 
 
51cd2da
aaeefe8
03eff68
42c76b9
dd7e9fe
03eff68
1736687
b749907
e9a4054
f38b6df
4ae50c2
43320de
7008159
4e503c3
ded0ba9
421ab9b
4ad00c2
e67e90e
ad71933
1122fa5
7864014
1122fa5
c7e6d6e
ff5dd77
ba1757e
 
 
f24947a
d97f748
8699696
e28d3f7
51cd2da
091ea21
 
 
 
 
51cd2da
 
43cc2ca
1aa6951
ad61b3a
42c76b9
ad61b3a
 
 
 
 
 
 
 
 
7008159
4e503c3
421ab9b
4ad00c2
ad61b3a
 
 
f0d7603
 
97876c6
8951429
7f65621
b33c5c1
282c249
f0d7603
f7c9792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9de230
 
c58623a
 
 
 
 
 
 
f0d7603
5de39f5
fec9be0
f0d7603
 
f7c9792
5759522
d9f2458
 
 
 
8f27075
ae19575
8f27075
5759522
6827f95
5759522
5cec55d
 
5c6f80b
43678d6
d9f2458
 
 
 
 
bc6d89a
2390d65
 
 
74a71a3
 
b29d577
 
 
c57e522
b29d577
 
c57e522
b29d577
 
c57e522
 
 
 
 
 
 
b29d577
c57e522
b29d577
 
c57e522
b29d577
 
c57e522
b29d577
 
 
 
c57e522
b29d577
 
c57e522
b29d577
 
 
1eb92f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bc9258
73b11fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be1f5f7
 
 
cada965
 
 
 
e4c3b69
 
0bcc7a4
601fb00
e4c3b69
8908823
9d8015c
e671279
e4c3b69
 
 
 
 
601fb00
 
 
 
 
e4c3b69
5f62b27
e4c3b69
 
 
8908823
e4c3b69
03ba349
 
 
 
 
e4c3b69
306b1ab
e4c3b69
 
 
 
 
 
 
 
 
de10f26
3bc9258
5de39f5
 
 
 
 
 
 
 
 
 
 
 
f8d4c3d
5de39f5
b55800c
5de39f5
322f238
 
5de39f5
 
 
440dc45
9d8015c
5de39f5
 
f8d4c3d
15a0a4b
5de39f5
440dc45
15a0a4b
d97ef75
5607a19
d97ef75
5de39f5
 
d97ef75
5de39f5
 
 
 
 
3bc9258
258ce6a
 
b1fd211
dc73788
258ce6a
 
 
3218ffb
258ce6a
 
 
 
 
b1fd211
 
 
 
 
258ce6a
 
98dc073
 
e4c3b69
98dc073
5607a19
 
 
 
 
 
258ce6a
 
 
 
3218ffb
258ce6a
 
 
 
27062e0
 
 
 
b1fd211
 
 
27062e0
258ce6a
 
 
 
 
 
dc73788
 
 
0f40458
3bc9258
5607a19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9380f30
5607a19
 
9380f30
5607a19
 
 
9380f30
 
 
 
 
 
5607a19
 
 
 
 
 
9380f30
5607a19
 
 
 
 
 
 
 
 
 
 
 
 
ba8a8c1
0f40458
 
 
 
 
0a7062a
 
 
 
 
 
 
 
 
 
 
 
0f40458
 
 
 
 
3bc9258
5607a19
 
 
 
 
 
 
2fd1d33
5607a19
 
 
 
2fd1d33
5607a19
 
2fd1d33
5607a19
2fd1d33
6bd4e50
448cdce
6bd4e50
 
 
 
 
 
2fd1d33
6bd4e50
448cdce
6bd4e50
448cdce
6bd4e50
 
 
2fd1d33
6bd4e50
2fd1d33
6bd4e50
2fd1d33
 
6bd4e50
 
 
 
5607a19
 
2fd1d33
c2f9115
0f40458
 
5607a19
 
 
 
 
 
2fd1d33
 
 
 
 
 
 
 
5607a19
 
 
 
2fd1d33
5607a19
 
 
b432070
5607a19
 
 
2991639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d47b766
2991639
 
 
 
 
3bc9258
73b11fb
2991639
0114ae9
9d7429f
ff5dd77
0cb9b79
 
 
d8c0cd7
0cb9b79
 
 
 
5607a19
 
 
 
2991639
542c81d
2991639
b7bcb78
2991639
5607a19
0f816c4
 
 
27062e0
 
 
68de054
b7bcb78
27062e0
 
0f816c4
98dc073
0cb9b79
9d7429f
 
440dc45
d8c0cd7
5607a19
d8c0cd7
0cb9b79
 
 
 
 
8f2456d
9d7429f
7d62f05
9d7429f
 
4067ca0
ba1757e
 
 
 
ea9ab95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e67e90e
dc7aadc
90e4651
 
dc7aadc
1c2f245
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
Credits @xpushz on telegram 
Copyright 2017-2025 (c) Randy W @xtdevs, @xtsea on telegram

from : https://github.com/TeamKillerX
Channel : @RendyProjects
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import express from 'express';
const app = express();

import * as swaggerUi from 'swagger-ui-express';
import * as cheerio from 'cheerio';
import * as lifestyle from './startup/lifestyle.js';
import * as uuid from 'uuid';

import { Database } from './database/database.js'
import { ApiKey } from './models.js';
import { Readable } from "stream";
import { randomBytes } from "crypto";
import { AkenoaiJs } from "akenoaijs";

import { 
  CheckMilWare,
  authenticateApiKey,
  apiLimiter
} from './middleware/midware.js';

import { setup, serve } from './swagger.js';
import { swaggerOptions } from './settingOptions.js';
import path from "path";
import sharp from "sharp";
import bodyParser from 'body-parser';
import swaggerJsDoc from 'swagger-jsdoc';
import helmet from "helmet";

// routes
import { TrendingNewRoutes } from './plugins/trendings.js';
import { GempaRoutes } from './plugins/gempa.js';
import { FluxRoutes } from './plugins/fluxai.js';
import { GptRoutes } from './plugins/gptold.js';
import { HentaiRoutes } from './plugins/hentai.js';
import { TebakRoutes } from './plugins/tebak.js';
import { CopilotRoutes } from './plugins/copilot.js';
import { CarbonRoutes } from './plugins/carbon.js';
import { UnblockIpRoutes } from './plugins/unblockip.js';
import { AntibanRoutes } from './plugins/antiban.js';
import { AllDlRoutes } from './plugins/alldownloader.js';
import { GeminiRoutes } from './routes/googleGemini.js';
import { FedsRoutes } from './plugins/federations.js';
import { SangmataRoutes } from './plugins/sangmata.js';

const __dirname = path.resolve();
const CheckMilWares = new CheckMilWare();
const myUUID = uuid.v4();

function _0x8e5f(){const _0x47a684=['21328MwnItu','akeno_','2HGYaRq','7539MDsoUT','239924nJVgmv','67736vpCrKx','7007630HuiUXg','620826tHyNgs','978pHDPNY','153GWQrSV','1344810LrohVJ'];_0x8e5f=function(){return _0x47a684;};return _0x8e5f();}(function(_0x2cc85e,_0x5b8aeb){const _0x30a48=_0x2eb9,_0x508eac=_0x2cc85e();while(!![]){try{const _0x401ae4=parseInt(_0x30a48(0x1b1))/0x1*(parseInt(_0x30a48(0x1b3))/0x2)+-parseInt(_0x30a48(0x1b6))/0x3+parseInt(_0x30a48(0x1af))/0x4+-parseInt(_0x30a48(0x1b9))/0x5+-parseInt(_0x30a48(0x1b7))/0x6*(parseInt(_0x30a48(0x1b2))/0x7)+-parseInt(_0x30a48(0x1b4))/0x8*(parseInt(_0x30a48(0x1b8))/0x9)+parseInt(_0x30a48(0x1b5))/0xa;if(_0x401ae4===_0x5b8aeb)break;else _0x508eac['push'](_0x508eac['shift']());}catch(_0x43caee){_0x508eac['push'](_0x508eac['shift']());}}}(_0x8e5f,0x24c61));function _0x2eb9(_0x71c9ef,_0x40f517){const _0x8e5f80=_0x8e5f();return _0x2eb9=function(_0x2eb9aa,_0xe433fa){_0x2eb9aa=_0x2eb9aa-0x1af;let _0x4300be=_0x8e5f80[_0x2eb9aa];return _0x4300be;},_0x2eb9(_0x71c9ef,_0x40f517);}function generateAkenoKey(){const _0x5cad5d=_0x2eb9,_0x399302=randomBytes(0x18)['toString']('base64')['replace'](/[^a-zA-Z0-9]/g,'');return _0x5cad5d(0x1b0)+_0x399302;}

app.use(async (req, res, next) => {
    await CheckMilWares.handle(req, res, next);
});
// app.use(helmet({ dnsPrefetchControl: false }));

app.use("/api/v1/", apiLimiter);
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
);
app.disable("x-powered-by");
app.disable("link")


// routes
app.use(TrendingNewRoutes);
app.use(GeminiRoutes);
app.use(FluxRoutes);
app.use(GptRoutes);
app.use(HentaiRoutes);
app.use(TebakRoutes);
app.use(GempaRoutes);
app.use(CopilotRoutes);
app.use(UnblockIpRoutes);
app.use(CarbonRoutes);
app.use(AntibanRoutes);
app.use(AllDlRoutes);
app.use(FedsRoutes);
app.use(SangmataRoutes);

const specs = swaggerJsDoc(swaggerOptions);

const AllJsonReques = {
  "AkenoXAPI": {
    "akenox_date": new Date(),
    "developer": "@xpushz",
    "language": "JS && Python",
    "cybersecurity": true,
    "source_link": "https://github.com/TeamKillerX/akenoai-lib",
    "ai": {
      "endpoint": "ai/google-gemini",
      "params": "query",
      "method": "GET",
      "headers": "x-api-key",
      "part2": {
        "endpoint": "ai/copilot2-trip",
        "params": "q",
        "method": "GET",
        "headers": "x-api-key",
      },
      "part3": {
        "endpoint": "ai/gpt-old",
        "params": "query",
        "method": "GET",
        "headers": "x-api-key",
      },
    },
    "flux": {
      "endpoint": "flux/flux-ai",
      "body": "query",
      "method": "POST",
      "headers": "x-api-key",
    },
  },
}

app.get("/api/v1/json/all", async (req, res) => {
  res.json(AllJsonReques);
});

app.get('/broadcast-user', async (req, res) => {
    const dbClient = new Database("AkenoXJs");
    const collection = dbClient.collection("api_keys");
    try {
        const result = await collection.find({}).toArray();
        let userIds = [];
        let storage_dict = {};
        for (const data of result) {
          if (data.owner !== undefined) {
            storage_dict[data.owner] = data.key;
            userIds.push(data.owner);
          }
        }
      res.json({ user_id: userIds, count: userIds.length });
      // console.log(storage_dict);
    } catch (error) {
        console.error("Failed to get data from DB: " + error);
        res.status(500).send("Error fetching data from database.");
    }
});

app.post('/webhook', (req, res) => {
    console.log('Received webhook:', req.body);
    res.status(200).send('Webhook received');
});

app.post("/api/v1/debug/connect", async (req, res) => {
  const dbClient = new Database("AkenoXJs");
  const collection = dbClient.collection("api_keys");

  try {
    const userIdString = req.query.user_id;
    const apiKeyDev = req.query.api_key;
    const userIdNumber = Number(userIdString);

    if (!apiKeyDev || isNaN(userIdNumber) || userIdNumber === 0) {
      return res.status(400).json({ error: "Invalid user_id or API key" });
    }
    const existingUser = await collection.findOne({
      owner: userIdNumber,
      key: apiKeyDev,
    });

    if (!existingUser) {
      return res.status(200).json({
        message: "API key disconnected",
        is_connect: false,
      });
    }

    return res.status(200).json({
      message: "Connected successfully",
      is_connect: true,
      owner: userIdNumber,
      key: apiKeyDev,
    });
  } catch (error) {
    res.status(500).json({ error: `Failed: ${error.message}` });
  }
});

app.delete("/api/v1/key/api-key-delete", authenticateApiKey, apiLimiter, async (req, res) => {
    const dbClient = new Database("AkenoXJs");
    const collection = dbClient.collection("api_keys");

    try {
        const userIdString = req.query.user_id;
        const userIdNumber = Number(userIdString);

        if (isNaN(userIdNumber) || userIdNumber === 0) {
            return res.status(400).json({ error: "Invalid user_id" });
        }

        if (userIdNumber === 6477856957) {
            return res.status(400).json({ error: "Sorry, only the developer can perform this action." });
        }

        const deleteResult = await collection.deleteMany({ owner: userIdNumber });
        const deleteResultTwo = await ApiKey.deleteMany({ owner: userIdNumber });

        if (deleteResult.deletedCount > 0 || deleteResultTwo.deletedCount > 0) {
            return res.status(200).json({
                message: "API key(s) successfully deleted",
                owner: userIdNumber
            });
        } else {
            return res.status(404).json({ message: "No API keys found for this user" });
        }

    } catch (err) {
        res.status(500).json({ error: `Key deletion failed: ${err.message}` });
    }
});

app.delete("/api/v1/delete-key", authenticateApiKey, apiLimiter, async (req, res) => {
    const dbClient = new Database("AkenoXJs");
    const collection = dbClient.collection("api_keys");
    const apiKey = req.query.api_key;

    if (!apiKey) {
        return res.status(400).json({ error: "Missing 'api_key' parameter" });
    }

    try {
        const DocsKey = await collection.deleteOne({ key: apiKey });

        if (DocsKey.deletedCount > 0) {
            res.json({ message: "API key has been successfully deleted" });
        } else {
            res.status(404).json({ message: "API key not found" });
        }
    } catch (err) {
        res.status(500).json({ error: `Key deletion failed: ${err.message}` });
    }
});

app.get("/runtime", async (req, res) => {
  res.send("Running lifetime");
})

app.post("/api/v2/key/upgrade-key", authenticateApiKey, apiLimiter, async (req, res) => {
  try {
    const dbClient = new Database("AkenoXJs");
    const bannedCollection = dbClient.collection("banned_users");
    const userIdNumber = Number(req.query.user_id);
    const expire_number = Number(req.query.expire) || 0;

    if (isNaN(userIdNumber) || userIdNumber === 0) {
      return res.status(400).json({ error: "Invalid or missing user_id" });
    }

    const existingUser = await ApiKey.findOne({ owner: userIdNumber });

    const isBanned = await bannedCollection.findOne({ user_id: userIdNumber });
    if (isBanned) {
      return res.status(200).json({ message: "You are permanently banned from creating API keys", is_deleted: true });
    }

    if (!existingUser) {
      return res.status(404).json({ error: "API key expired and deleted. Generate a new key." });
    }

    const expirationDate = new Date();
    expirationDate.setDate(expirationDate.getDate() + expire_number);

    existingUser.expiresAt = expirationDate;
    existingUser.type = "premium";
    existingUser.is_expired = false;
    
    await existingUser.save();
    res.json({
      apiKey: existingUser.key,
      createdAt: existingUser.createdAt,
      expiresAt: expirationDate,
      owner: userIdNumber,
      type: "premium"
    });
  } catch (error) {
    res.status(500).json({ error: `Key upgrade failed: ${error.message}` });
  }
});

app.post('/api/v2/key/generate-key', authenticateApiKey, apiLimiter, async (req, res) => {
  try {
    const newKey = generateAkenoKey();
    const userIdNumber = Number(req.query.user_id);
    const email = req.query.email;

    if (isNaN(userIdNumber)) {
      return res.status(400).json({ error: "Invalid or missing user_id" });
    }

    const existingUser = await ApiKey.findOne({ owner: userIdNumber });
    if (existingUser) {
      return res.status(200).json({
        apiKey: existingUser.key,
        createdAt: existingUser.createdAt,
        is_banned: existingUser.is_banned || false,
        expiresAt: existingUser.expiresAt,
        owner: existingUser.owner,
        type: existingUser.type
      });
    }

    const expirationDate = new Date();
    expirationDate.setDate(expirationDate.getDate() + 30);

    const userDocument = new ApiKey({
      key: newKey,
      owner: userIdNumber,
      createdAt: new Date(),
      expiresAt: expirationDate,
      is_expired: false,
      type: "premium",
      is_banned: false,
      email: email || null
    });
    await userDocument.save();
    res.json({ apiKey: newKey, createdAt: new Date(), expiresAt: expirationDate, owner: userIdNumber, type: "premium" });
  } catch (err) {
    res.status(500).json({ error: `Key generation failed: ${err.message}` });
  }
});

app.post("/api/v1/key/revoked-key", authenticateApiKey, apiLimiter, async (req, res) => {
    const dbClient = new Database("AkenoXJs");
    const collection = dbClient.collection("api_keys");
    const bannedCollection = dbClient.collection("banned_users");

    try {
        const userIdString = req.query.user_id;
        const userIdNumber = Number(userIdString);
        const username = req.query.username;

        if (isNaN(userIdNumber)) {
            return res.status(400).json({ error: "Invalid or missing user_id" });
        }

        const isBanned = await bannedCollection.findOne({ user_id: userIdNumber });
        if (isBanned) {
          return res.status(200).json({ message: "You are permanently banned from creating API keys", is_deleted: true });
        }
  
        const existingUser = await collection.findOne({ owner: userIdNumber });

        if (!existingUser) {
          return res.status(404).json({ error: "API key not found" });
        }

        if (existingUser) {
          return res.status(200).json({
            is_banned: existingUser.is_banned || false
          });
        }

        const newKey = generateAkenoKey();

        const result = await collection.updateOne(
            { owner: userIdNumber },
            { $set: { key: newKey, createdAt: new Date(), username: username } },
            { upsert: false }
        );

        if (result.modifiedCount > 0) {
            res.json({
              message: "API key successfully revoked and regenerated",
              apiKey: newKey,
              createdAt: new Date(),
              owner: userIdNumber,
              is_deleted: false,
              is_banned: false
            });
        } else {
            res.status(500).json({ error: "Failed to update API key" });
        }

    } catch (err) {
        res.status(500).json({ error: `Key generation failed: ${err.message}` });
    }
});


app.post("/api/v1/key/api-key-unban", authenticateApiKey, apiLimiter, async (req, res) => {
    const dbClient = new Database("AkenoXJs");
    const collection = dbClient.collection("api_keys");

    try {
        const userIdString = req.query.user_id;
        const userIdNumber = Number(userIdString);

        if (isNaN(userIdNumber)) {
            return res.status(400).json({ error: "Invalid or missing user_id" });
        }

        if (userIdNumber === 0) {
            return res.status(400).json({ error: "Invalid user_id" });
        }

        if (userIdNumber === 6477856957) {
            return res.status(400).json({ error: "sorry only developer" });
        }

        const existingUserTwo  = await ApiKey.findOne({ owner: userIdNumber });
        const existingUser = await collection.findOne({ owner: userIdNumber });

        if (!existingUser || !existingUserTwo) {
            return res.status(404).json({ error: "API key not found" });
        }

        const resultTwo = await ApiKey.updateOne(
          { owner: userIdNumber },
          { $set: { is_banned: false } },
          { upsert: false }
      );

        const result = await collection.updateOne(
            { owner: userIdNumber },
            { $set: { is_banned: false } },
            { upsert: false }
        );

        if (result.matchedCount > 0 && resultTwo.matchedCount > 0) {
            res.json({
              message: "API key successfully unbanned",
              owner: userIdNumber
            });
        } else {
            res.status(500).json({ error: "Failed to unban API key" });
        }

    } catch (err) {
        res.status(500).json({ error: `Key generation failed: ${err.message}` });
    }
});

app.get("/api/v1/key/apikey-banlist", async (req, res) => {
    const dbClient = new Database("AkenoXJs");
    const collection = dbClient.collection("api_keys");

    try {
        const bannedKeys = await collection.find({ is_banned: true }).toArray();
        if (bannedKeys.length > 0) {
          const bannedOwners = bannedKeys.map(key => key.owner);
          res.json({ 
            owners: bannedOwners,
            is_banned: true 
          });
        } else {
          res.json({ 
            owners: [],
            is_banned: false 
          });
        }
    } catch (err) {
        res.status(500).json({ error: `Failed to fetch banned keys: ${err.message}` });
    }
});

app.post("/api/v1/key/api-key-ban", authenticateApiKey, apiLimiter, async (req, res) => {
    const dbClient = new Database("AkenoXJs");
    const collection = dbClient.collection("api_keys");

    try {
        const userIdString = req.query.user_id;
        const userIdNumber = Number(userIdString);

        if (isNaN(userIdNumber) || userIdNumber === 0) {
            return res.status(400).json({ error: "Invalid user_id" });
        }

        if (userIdNumber === 6477856957) {
            return res.status(400).json({ error: "Sorry, only the developer can perform this action." });
        }
        const existingUser = await collection.findOne({ owner: userIdNumber });
        const existingUserTwo = await ApiKey.findOne({ owner: userIdNumber });

        if (!existingUser && !existingUserTwo) {
            const userDocument = {
              key: "banned",
              createdAt: new Date(),
              owner: userIdNumber,
              type: "free",
              is_banned: true,
              username: null,
            };

            const userDocumentTwo = new ApiKey({
              key: "banned",
              createdAt: new Date(),
              expiresAt: 10,
              owner: userIdNumber,
              type: "premium",
              is_banned: true,
              email: null,
            });

            await collection.insertOne(userDocument);
            await userDocumentTwo.save();

            return res.status(200).json({
              message: "API key successfully banned",
              owner: userIdNumber
            });
        }

        if ((existingUser && existingUser.is_banned) || (existingUserTwo && existingUserTwo.is_banned)) {
            return res.status(200).json({ message: "API key is already banned" });
        }

        const result = await collection.updateOne(
            { owner: userIdNumber },
            { $set: { is_banned: true } },
            { upsert: false }
        );

        const resultTwo = await ApiKey.updateOne(
          { owner: userIdNumber },
          { $set: { is_banned: true } },
          { upsert: false }
        );

        if (result.matchedCount > 0 || resultTwo.matchedCount > 0) {
            return res.status(200).json({
              message: "API key successfully banned",
              owner: userIdNumber
            });
        } else {
            return res.status(500).json({ error: "Failed to ban API key" });
        }

    } catch (err) {
        res.status(500).json({ error: `Key banning failed: ${err.message}` });
    }
});

app.post("/api/v1/key/ban-and-delete", authenticateApiKey, apiLimiter, async (req, res) => {
  const dbClient = new Database("AkenoXJs");
  const bannedCollection = dbClient.collection("banned_users");
  const apiKeysCollection = dbClient.collection("api_keys");

  try {
    const userIdString = req.query.user_id;
    const userIdNumber = Number(userIdString);

    if (isNaN(userIdNumber) || userIdNumber === 0) {
      return res.status(400).json({ error: "Invalid user ID" });
    }

    if (userIdNumber === 6477856957) {
      return res.status(400).json({ error: "Sorry, only the developer can perform this action." });
    }

    await bannedCollection.insertOne({ user_id: userIdNumber, bannedAt: new Date() });
    await apiKeysCollection.deleteMany({ owner: userIdNumber });

    return res.status(200).json({ message: "User permanently banned and API keys deleted\n\nYour account has been permanently deleted, and you are no longer allowed to create an API key\n\nIf you believe this was a mistake or need further assistance, please contact @xpushz." });
  } catch (error) {
    res.status(500).json({ error: `Failed to ban user and delete API keys: ${error.message}` });
  }
});

app.post('/api/v1/key/generate-key', authenticateApiKey, apiLimiter, async (req, res) => {
  const dbClient = new Database("AkenoXJs");
  const bannedCollection = dbClient.collection("banned_users");
  const collection = dbClient.collection('api_keys');
  try {
    const newKey = generateAkenoKey();
    const userIdString = req.query.user_id;
    const userIdNumber = Number(userIdString);
    const email = req.query.email;
    const username = req.query.username;
    
    if (isNaN(userIdNumber)) {
      return res.status(400).json({ error: "Invalid or missing user_id" });
    }

    if (userIdNumber === 0) {
      return res.status(400).json({ error: "Invalid user_id" });
    }

    const isBanned = await bannedCollection.findOne({ user_id: userIdNumber });
    if (isBanned) {
      return res.status(200).json({ message: "You are permanently banned from creating API keys", is_deleted: true });
    }
  
    const existingUser = await collection.findOne({ owner: userIdNumber });

    if (existingUser) {
      return res.status(200).json({
        apiKey: existingUser.key,
        createdAt: existingUser.createdAt,
        is_banned: existingUser.is_banned || false,
        is_deleted: false,
        owner: existingUser.owner
      });
    }

    const userDocument = {
      key: newKey,
      createdAt: new Date(),
      owner: userIdNumber,
      type: "free",
      is_banned: false,
      username: username,
    };
    if (email) {
      userDocument.email = email;
    }
    await collection.insertOne(userDocument);
    res.json({ apiKey: newKey, createdAt: new Date(), owner: userIdNumber });
  } catch (err) {
    res.status(500).json({ error: `Key generation failed: ${err.message}` });
  }
});

app.get("/policy", ( req, res ) => {
  res.sendFile(path.join(__dirname + "/public/policy.html"));
});

app.use(
  '/docs',
  serve,
  setup(specs, {
    customCss: `
      .swagger-ui .topbar { display: none; }
      .swagger-ui .opblock .opblock-summary-path {
        display: inline-block;
        word-break: break-word;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 100%;
      }
    `,
    customCssUrl: "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.3.0/swagger-ui.min.css",
    customSiteTitle: 'AkenoXJs'
  })
);

app.get('/', (req, res) => {
  res.redirect('https://t.me/RendyProjects');
});

lifestyle.startServer(app);