hanxuan commited on
Commit
79902d0
1 Parent(s): b4a2f88

添加开关,隐藏IP

Browse files
Files changed (1) hide show
  1. app.js +14 -2
app.js CHANGED
@@ -1,5 +1,6 @@
1
  const express = require('express');
2
  const axios = require('axios');
 
3
 
4
  const app = express();
5
  const port = 7860;
@@ -16,10 +17,21 @@ const timeout = 30000; // 访问超时
16
 
17
  // 使用中间件,接收所有请求
18
  app.use((req, res, next) => {
19
- const userIP = req.headers['x-forwarded-for'] || req.socket.remoteAddress || null;
 
 
 
 
 
 
 
 
 
 
 
20
  const currentDate = new Date().toLocaleString('zh-CN', { hour12: false });
21
  //console.log(`[${currentDate}] 收到来自 IP ${userIP} 的请求`);
22
- res.locals.userIP = userIP; // 将 userIP 存储在 res.locals 中,以便在后续中间件和路由中使用
23
  next();
24
  });
25
 
 
1
  const express = require('express');
2
  const axios = require('axios');
3
+ const SHOW_FULL_IP = false;
4
 
5
  const app = express();
6
  const port = 7860;
 
17
 
18
  // 使用中间件,接收所有请求
19
  app.use((req, res, next) => {
20
+ let userIP = req.headers['x-forwarded-for'] || req.socket.remoteAddress || null;
21
+
22
+ // 如果 SHOW_FULL_IP 为 false,则隐藏部分 IP 地址
23
+ if (!SHOW_FULL_IP) {
24
+ const parts = userIP.split('.');
25
+ if (parts.length === 4) {
26
+ parts[2] = 'xxx';
27
+ parts[3] = 'xxx';
28
+ }
29
+ userIP = parts.join('.');
30
+ }
31
+
32
  const currentDate = new Date().toLocaleString('zh-CN', { hour12: false });
33
  //console.log(`[${currentDate}] 收到来自 IP ${userIP} 的请求`);
34
+ res.locals.userIP = userIP;
35
  next();
36
  });
37