Ver Fonte

增加接口请求ip校验

JiahengHe há 3 meses atrás
pai
commit
14fbb698d2
1 ficheiros alterados com 55 adições e 1 exclusões
  1. 55 1
      src/web.js

+ 55 - 1
src/web.js

@@ -43,10 +43,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) => {
@@ -70,9 +81,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;
@@ -122,6 +145,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);
@@ -141,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)
@@ -180,6 +215,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 = []
@@ -227,6 +268,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"});