浏览代码

新增web 请求限制,需要携带请求头

hl 1 年之前
父节点
当前提交
bb1bdeef72
共有 1 个文件被更改,包括 21 次插入2 次删除
  1. 21 2
      src/web.js

+ 21 - 2
src/web.js

@@ -8,6 +8,7 @@ const {spawn, exec, fork, execSync} = require("child_process");
 const path = require("path");
 const {ka} = require("date-fns/locale");
 const {getAppMap} = require("./robot");
+const crypto = require('crypto');
 
 /*******
  * web
@@ -17,7 +18,25 @@ function init() {
     const app = express();
     const port = 3000;
     // 中间件示例,用于处理JSON请求体
-    app.use(express.json());
+    // app.use(express.json());
+    app.use((req, res, next) => {
+        var k = "$8gBV!f&L@E"
+        if ("timestamp" in req.headers) {
+            var timestamp = req.headers.timestamp
+            var md5 = req.headers.catilla
+            //默认32位
+            const md5Str = crypto.createHash('md5').update(k + timestamp).digest('hex');
+            // logger.info(JSON.stringify(md5Str))
+            if (md5 === md5Str) {
+                next();
+            } else {
+                res.status(403).send('拒绝访问!');
+            }
+        } else {
+            res.status(403).send('没有访问权限!');
+        }
+    });
+
 
     // GET请求的路由示例
     app.get('/', (req, res) => {
@@ -30,7 +49,7 @@ function init() {
             // str.set(key,JSON.stringify(value))
             s[key] = value
         })
-        res.send({'code': 200, 'data': JSON.stringify(s), "message": "SUCCESS"} );
+        res.send({'code': 200, 'data': JSON.stringify(s), "message": "SUCCESS"});
     });