randydev commited on
Commit
88b8ef4
·
verified ·
1 Parent(s): 35b51b7

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +19 -4
index.js CHANGED
@@ -8,15 +8,30 @@ app.get('/', (req, res) => {
8
  });
9
 
10
  app.use((req, res, next) => {
11
- const ip = req.ip;
12
- const userAgent = req.get("User-Agent");
13
- console.log(`Request from IP address: ${ip}\n\nUser Agent: ${userAgent}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  next();
15
  });
16
 
17
  app.get('/api/test', async (req, res) => {
18
  try {
19
- console.log("Test", req)
20
  res.send("Hello world")
21
  } catch (error) {
22
  res.status(401).json({error: error.message})
 
8
  });
9
 
10
  app.use((req, res, next) => {
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
+
15
+ let realIP = req.ip;
16
+
17
+ if (xForwardedFor) {
18
+ realIP = xForwardedFor.split(',')[0].trim();
19
+ }
20
+ else if (xRealIP){
21
+ realIP = xRealIP;
22
+ }
23
+ else if (cfConnectingIP) {
24
+ realIP = cfConnectingIP;
25
+ }
26
+
27
+ req.realIP = realIP;
28
+ console.log(`Real IP address is: ${realIP}, header: ${xForwardedFor ? "x-forwarded-for" : xRealIP ? "x-real-ip" : cfConnectingIP ? "cf-connecting-ip" : "req.ip"} `);
29
  next();
30
  });
31
 
32
  app.get('/api/test', async (req, res) => {
33
  try {
34
+ // console.log("Test", req)
35
  res.send("Hello world")
36
  } catch (error) {
37
  res.status(401).json({error: error.message})