Просмотр исходного кода

交互密码由WEB前端放到后端校检

niujiuru 2 дней назад
Родитель
Сommit
15148dd9ab

+ 3 - 0
web-ui.service/page1_handler.go

@@ -674,6 +674,9 @@ func downloadUpgradeLog(w http.ResponseWriter, r *http.Request) {
 func getUpgradeConfig(w http.ResponseWriter, r *http.Request) {
 	switch r.Method {
 	case http.MethodGet:
+		if !checkOperationPassword(w, r) {
+			return
+		}
 		callRPCResponse(w, r, 7005, "core.getUpgradeConf", nil)
 	default:
 		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)

+ 8 - 5
web-ui.service/web/static/js/page1.js

@@ -675,14 +675,17 @@ function viewUpgradeConf() {
     if (password === null) {
         return;
     }
-    if (password !== "yfkj123456") {
-        alert("操作密码错误!");
-        return;
-    }
 
-    fetch("/api/upgrade/getUpgradeConf")
+    fetch("/api/upgrade/getUpgradeConf", {
+        headers: {
+            "X-Operation-Password": password
+        }
+    })
         .then(function (r) {
             if (!r.ok) {
+                if (r.status === 401) {
+                    throw new Error("操作密码错误");
+                }
                 throw new Error("HTTP " + r.status);
             }
             return r.json();

+ 10 - 0
web-ui.service/web_handler.go

@@ -3,6 +3,7 @@ package main
 import (
 	"bufio"
 	"context"
+	"crypto/subtle"
 	"encoding/json"
 	"fmt"
 	"io/fs"
@@ -505,3 +506,12 @@ func serviceControlHandler(w http.ResponseWriter, r *http.Request) {
 
 	json.NewEncoder(w).Encode(map[string]bool{"ok": true})
 }
+
+func checkOperationPassword(w http.ResponseWriter, r *http.Request) bool {
+	password := r.Header.Get("X-Operation-Password")
+	if subtle.ConstantTimeCompare([]byte(password), []byte("yfkj123456")) != 1 {
+		http.Error(w, "操作密码错误", http.StatusUnauthorized)
+		return false
+	}
+	return true
+}