Update midware.js
Browse files- midware.js +26 -14
midware.js
CHANGED
@@ -1,15 +1,19 @@
|
|
1 |
var Database = require('./database.js');
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
|
|
|
|
|
|
|
8 |
const xForwardedFor = req.headers['x-forwarded-for'];
|
9 |
const xRealIP = req.headers['x-real-ip'];
|
10 |
const cfConnectingIP = req.headers['cf-connecting-ip'];
|
11 |
-
let realIP = req.ip;
|
12 |
-
|
|
|
13 |
if (xForwardedFor) {
|
14 |
realIP = xForwardedFor.split(',')[0].trim();
|
15 |
} else if (xRealIP) {
|
@@ -18,29 +22,37 @@ const CheckMilWare = async (app) => {
|
|
18 |
realIP = cfConnectingIP;
|
19 |
}
|
20 |
|
|
|
21 |
req.realIP = realIP;
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
if (isBlocked && isBlocked.blocked === true) {
|
25 |
return res.status(403).send("Access denied: IP is blocked");
|
26 |
}
|
27 |
|
|
|
28 |
if (req.path === '/env') {
|
29 |
console.log("Check path /env");
|
30 |
-
await dbClient.AddIpisBlocked(realIP);
|
31 |
return res.status(403).send("Access denied: IP is blocked..");
|
32 |
}
|
33 |
|
34 |
-
await dbClient.IPAddressAndUpdate(realIP);
|
35 |
-
|
36 |
-
console.log(`Real IP address is: ${realIP}, header: ${xForwardedFor ? "x-forwarded-for" : xRealIP ? "x-real-ip" : cfConnectingIP ? "cf-connecting-ip" : "req.ip"}`);
|
37 |
|
38 |
next();
|
39 |
} catch (error) {
|
40 |
console.error("Error in middleware: " + error);
|
41 |
res.status(500).send("Something bad happened");
|
42 |
}
|
43 |
-
}
|
44 |
-
}
|
45 |
|
46 |
-
module.exports =
|
|
|
1 |
var Database = require('./database.js');
|
2 |
|
3 |
+
class CheckMilWare {
|
4 |
+
constructor() {
|
5 |
+
this.dbClient = new Database("AkenoXJs", "FastJsAPI");
|
6 |
+
}
|
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 |
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 |
+
|
35 |
if (isBlocked && isBlocked.blocked === true) {
|
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 |
|
50 |
next();
|
51 |
} catch (error) {
|
52 |
console.error("Error in middleware: " + error);
|
53 |
res.status(500).send("Something bad happened");
|
54 |
}
|
55 |
+
}
|
56 |
+
}
|
57 |
|
58 |
+
module.exports = CheckMilWare;
|