Browse Source

加入中控ip校验,只接受该ip的请求

JiahengHe 3 months ago
parent
commit
7f518ef6f6
1 changed files with 57 additions and 2 deletions
  1. 57 2
      src/web.js

+ 57 - 2
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);
@@ -131,7 +160,8 @@ function init() {
                 await robot.run(param)
             } else if (executeType === "STOP") {
                 await robot.closeApp(param)
-            } else if (executeType === "RESTART") {
+            } else if (executeType === "RESTA" +
+                "RT") {
                 await robot.restartApp(param)
             }
         })(param);
@@ -140,6 +170,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 +183,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 = []
@@ -194,6 +236,19 @@ function robotNodeStatus() {
     });
 }
 
+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;
+}
 
 function isOK(req, res) {
     res.send({'code': 200, 'data': "null", "message": "SUCCESS"});