JiahengHe před 3 měsíci
rodič
revize
3ddd3eb220
1 změnil soubory, kde provedl 56 přidání a 1 odebrání
  1. 56 1
      src/web.js

+ 56 - 1
src/web.js

@@ -42,10 +42,21 @@ function init() {
 
     // GET请求的路由示例
     app.get('/', (req, res) => {
+        let checkIp = check_ip(req);
+        if (!checkIp) {
+            // 回应信号(响应请求)
+            res.send({'code': 900, 'data': "null", "message": "非法访问"});
+            return;// 终止函数执行
+        }
         res.send('is OK!');
     });
     app.get('/robotList', (req, res) => {
-
+        let checkIp = check_ip(req);
+        if (!checkIp) {
+            // 回应信号(响应请求)
+            res.send({'code': 900, 'data': "null", "message": "非法访问"});
+            return;// 终止函数执行
+        }
         var map = robot.getAppMap()
         var list = new Array();
         robot.appMap.forEach((value, key) => {
@@ -69,9 +80,21 @@ function init() {
 
     //简单三个接口,为了方便使用get请求
     app.get('/isOK', (req, res) => {
+        let checkIp = check_ip(req);
+        if (!checkIp) {
+            // 回应信号(响应请求)
+            res.send({'code': 900, 'data': "null", "message": "非法访问"});
+            return;// 终止函数执行
+        }
         res.send({'code': 200, 'data': true, "message": "SUCCESS"});
     });
     app.get('/logs', (req, res) => {
+        let checkIp = check_ip(req);
+        if (!checkIp) {
+            // 回应信号(响应请求)
+            res.send({'code': 900, 'data': "null", "message": "非法访问"});
+            return;// 终止函数执行
+        }
         // logger.info(`---------------------1`)
         // 获取发送过来的信息(请求体的数据)
         const param = req.query;
@@ -121,6 +144,12 @@ function init() {
         // }
     });
     app.post('/execute', (req, res) => {
+        let checkIp = check_ip(req);
+        if (!checkIp) {
+            // 回应信号(响应请求)
+            res.send({'code': 900, 'data': "null", "message": "非法访问"});
+            return;// 终止函数执行
+        }
         // 获取发送过来的信息(请求体的数据)
         const param = req.body;
         // logger.info('--web 启动',param);
@@ -140,6 +169,12 @@ function init() {
     })
     //启动-仓位检查
     app.get('/searchPositions', (req, res) => {
+        let checkIp = check_ip(req);
+        if (!checkIp) {
+            // 回应信号(响应请求)
+            res.send({'code': 900, 'data': "null", "message": "非法访问"});
+            return;// 终止函数执行
+        }
         const param = req.query;
         // logger.info(JSON.stringify(param));
         robot.searchPositions(param)
@@ -147,6 +182,12 @@ function init() {
     })
     // 新策略-返回机器人状态
     app.get('/predictorState', (req, res) => {
+        let checkIp = check_ip(req);
+        if (!checkIp) {
+            // 回应信号(响应请求)
+            res.send({'code': 900, 'data': "null", "message": "非法访问"});
+            return;// 终止函数执行
+        }
         const param = req.query;
         const id = param.id
         let result = []
@@ -199,6 +240,20 @@ function isOK(req, res) {
     res.send({'code': 200, 'data': "null", "message": "SUCCESS"});
 }
 
+function check_ip(req){
+    let ip = req.headers['x-forwarded-for'] || req.ip || req.socket.remoteAddress;
+    if (ip && ip.includes(',')) ip = ip.split(',')[0].trim();
+
+    // 移除IPv6前缀(如存在)
+    ip = ip.replace('::ffff:', '');
+    // 指定中控ip
+    if (ip !== '52.194.207.233') {
+        logger.info(`非法来源IP:  `, ip);
+        return false;
+    }
+    return true;
+}
+
 // 日志读取操作
 module.exports = {
     init,