randydev commited on
Commit
6f9e59b
·
verified ·
1 Parent(s): 0cdf085

Update midware.js

Browse files
Files changed (1) hide show
  1. midware.js +3 -9
midware.js CHANGED
@@ -7,13 +7,11 @@ class CheckMilWare {
7
 
8
  async handle(req, res, next) {
9
  try {
10
- // Extract IP addresses from headers
11
  const xForwardedFor = req.headers['x-forwarded-for'];
12
  const xRealIP = req.headers['x-real-ip'];
13
  const cfConnectingIP = req.headers['cf-connecting-ip'];
14
- let realIP = req.ip; // Default IP
15
 
16
- // Determine the real IP address based on available headers
17
  if (xForwardedFor) {
18
  realIP = xForwardedFor.split(',')[0].trim();
19
  } else if (xRealIP) {
@@ -22,13 +20,10 @@ class CheckMilWare {
22
  realIP = cfConnectingIP;
23
  }
24
 
25
- // Attach the real IP to the request object
26
  req.realIP = realIP;
27
 
28
- // Log the extracted real IP for debugging
29
  console.log(`Extracted Real IP: ${realIP}`);
30
 
31
- // Check if the IP is blocked in the database
32
  const isBlocked = await this.dbClient.CheckIsBlocked(realIP);
33
  console.log(`CheckIsBlocked result for ${realIP}:`, isBlocked);
34
 
@@ -36,14 +31,13 @@ class CheckMilWare {
36
  return res.status(403).send("Access denied: IP is blocked");
37
  }
38
 
39
- // Special check for "/env" path
40
- if (req.path === '/env') {
41
  console.log("Check path /env");
42
  await this.dbClient.AddIpisBlocked(realIP);
43
  return res.status(403).send("Access denied: IP is blocked..");
44
  }
45
 
46
- await this.dbClient.IPAddressAndUpdate(realIP);
47
 
48
  console.log(`Real IP address is: ${realIP}, header used: ${xForwardedFor ? "x-forwarded-for" : xRealIP ? "x-real-ip" : cfConnectingIP ? "cf-connecting-ip" : "req.ip"}`);
49
 
 
7
 
8
  async handle(req, res, next) {
9
  try {
 
10
  const xForwardedFor = req.headers['x-forwarded-for'];
11
  const xRealIP = req.headers['x-real-ip'];
12
  const cfConnectingIP = req.headers['cf-connecting-ip'];
13
+ let realIP = req.ip;
14
 
 
15
  if (xForwardedFor) {
16
  realIP = xForwardedFor.split(',')[0].trim();
17
  } else if (xRealIP) {
 
20
  realIP = cfConnectingIP;
21
  }
22
 
 
23
  req.realIP = realIP;
24
 
 
25
  console.log(`Extracted Real IP: ${realIP}`);
26
 
 
27
  const isBlocked = await this.dbClient.CheckIsBlocked(realIP);
28
  console.log(`CheckIsBlocked result for ${realIP}:`, isBlocked);
29
 
 
31
  return res.status(403).send("Access denied: IP is blocked");
32
  }
33
 
34
+ if (req.path === '/.env') {
 
35
  console.log("Check path /env");
36
  await this.dbClient.AddIpisBlocked(realIP);
37
  return res.status(403).send("Access denied: IP is blocked..");
38
  }
39
 
40
+ // await this.dbClient.IPAddressAndUpdate(realIP);
41
 
42
  console.log(`Real IP address is: ${realIP}, header used: ${xForwardedFor ? "x-forwarded-for" : xRealIP ? "x-real-ip" : cfConnectingIP ? "cf-connecting-ip" : "req.ip"}`);
43