randydev commited on
Commit
0ff459c
·
verified ·
1 Parent(s): c8506c4

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +7 -62
index.js CHANGED
@@ -2,12 +2,14 @@ const express = require('express')
2
  const app = express()
3
  const port = 7860
4
  const axios = require('axios')
5
- const { connectToMongoDB } = require('./database');
6
 
 
 
7
  const startup = async () => {
8
  try {
9
  console.log("Starting application...");
10
- await connectToMongoDB();
11
  console.log("MongoDB connected successfully.");
12
  } catch (error) {
13
  console.error("Error during startup:", error.message);
@@ -22,63 +24,6 @@ const startServer = async () => {
22
  });
23
  };
24
 
25
- const IPAddressAndUpdate = async (ip) => {
26
- try {
27
- const db = await connectToMongoDB();
28
- const collection = db.collection("FastJsAPI");
29
- const filter = { ip: ip };
30
- const update = { $set: { ip: ip } };
31
- const result = await collection.updateOne(filter, update, { upsert: true });
32
-
33
- if (result.upsertedCount > 0) {
34
- console.log("Inserted a new IP address:", ip);
35
- } else {
36
- console.log("Updated an existing IP address:", ip);
37
- }
38
- } catch (error) {
39
- console.error("Error updating IP address:", error.message);
40
- }
41
- };
42
-
43
- const AddIpisBlocked = async (ip) => {
44
- try {
45
- const db = await connectToMongoDB();
46
- const collection = db.collection("FastJsAPI");
47
- const filter = { ip: ip };
48
- const update = { $set: { blocked: true } };
49
- const result = await collection.updateOne(filter, update, { upsert: true });
50
-
51
- if (result.upsertedCount > 0) {
52
- console.log("Inserted a new IP address:", ip);
53
- } else {
54
- console.log("Updated an existing IP address:", ip);
55
- }
56
- } catch (error) {
57
- console.error("Error updating IP address:", error.message);
58
- }
59
- };
60
-
61
-
62
- const CheckIsBlocked = async (ip) => {
63
- try {
64
- const db = await connectToMongoDB();
65
- const collection = db.collection("FastJsAPI");
66
- const filter = { ip: ip };
67
- const FindIp = await collection.findOne(filter);
68
-
69
- if (FindIp) {
70
- console.log("IP found in the database:", FindIp);
71
- return FindIp;
72
- } else {
73
- console.log("IP not found in the database");
74
- return null;
75
- }
76
- } catch (error) {
77
- console.error("Error checking IP:", error.message);
78
- return null;
79
- }
80
- };
81
-
82
  app.get('/', (req, res) => {
83
  res.redirect('https://t.me/RendyProjects');
84
  });
@@ -101,17 +46,17 @@ app.use(async (req, res, next) => {
101
  }
102
 
103
  req.realIP = realIP;
104
- const isBlocked = await CheckIsBlocked(realIP)
105
  if (isBlocked && isBlocked.blocked === true) {
106
  return res.status(403).send("Access denied: IP is blocked");
107
  }
108
  if (req.path === '/env') {
109
  console.log("check path /env");
110
- await AddIpisBlocked(realIP)
111
  return res.status(403).send("Access denied: IP is blocked..");
112
  }
113
 
114
- await IPAddressAndUpdate(realIP);
115
  console.log(`Real IP address is: ${realIP}, header: ${xForwardedFor ? "x-forwarded-for" : xRealIP ? "x-real-ip" : cfConnectingIP ? "cf-connecting-ip" : "req.ip"} `);
116
  await next();
117
  });
 
2
  const app = express()
3
  const port = 7860
4
  const axios = require('axios')
5
+ const { Database } = require('./database');
6
 
7
+ const dbClient = new Database("AkenoXJs", "FastJsAPI");
8
+
9
  const startup = async () => {
10
  try {
11
  console.log("Starting application...");
12
+ await dbClient.connect();
13
  console.log("MongoDB connected successfully.");
14
  } catch (error) {
15
  console.error("Error during startup:", error.message);
 
24
  });
25
  };
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  app.get('/', (req, res) => {
28
  res.redirect('https://t.me/RendyProjects');
29
  });
 
46
  }
47
 
48
  req.realIP = realIP;
49
+ const isBlocked = await dbClient.CheckIsBlocked(realIP)
50
  if (isBlocked && isBlocked.blocked === true) {
51
  return res.status(403).send("Access denied: IP is blocked");
52
  }
53
  if (req.path === '/env') {
54
  console.log("check path /env");
55
+ await dbClient.AddIpisBlocked(realIP)
56
  return res.status(403).send("Access denied: IP is blocked..");
57
  }
58
 
59
+ await dbClient.IPAddressAndUpdate(realIP);
60
  console.log(`Real IP address is: ${realIP}, header: ${xForwardedFor ? "x-forwarded-for" : xRealIP ? "x-real-ip" : cfConnectingIP ? "cf-connecting-ip" : "req.ip"} `);
61
  await next();
62
  });