浏览代码

warp - optimize utility code

mhsanaei 3 周之前
父节点
当前提交
280a22b57d
共有 2 个文件被更改,包括 10 次插入13 次删除
  1. 2 2
      web/html/xui/warp_modal.html
  2. 8 11
      web/service/warp.go

+ 2 - 2
web/html/xui/warp_modal.html

@@ -176,10 +176,10 @@
             },
             async register() {
                 warpModal.loading(true);
-                keys = Wireguard.generateKeypair();
+                const keys = Wireguard.generateKeypair();
                 const msg = await HttpUtil.post('/panel/xray/warp/reg', keys);
                 if (msg.success) {
-                    resp = JSON.parse(msg.obj);
+                    const resp = JSON.parse(msg.obj);
                     warpModal.warpData = resp.data;
                     warpModal.warpConfig = resp.config;
                     this.collectConfig();

+ 8 - 11
web/service/warp.go

@@ -56,8 +56,7 @@ func (s *WarpService) GetWarpConfig() (string, error) {
 		return "", err
 	}
 	defer resp.Body.Close()
-	buffer := bytes.NewBuffer(make([]byte, 8192))
-	buffer.Reset()
+	buffer := &bytes.Buffer{}
 	_, err = buffer.ReadFrom(resp.Body)
 	if err != nil {
 		return "", err
@@ -87,14 +86,13 @@ func (s *WarpService) RegWarp(secretKey string, publicKey string) (string, error
 		return "", err
 	}
 	defer resp.Body.Close()
-	buffer := bytes.NewBuffer(make([]byte, 8192))
-	buffer.Reset()
+	buffer := &bytes.Buffer{}
 	_, err = buffer.ReadFrom(resp.Body)
 	if err != nil {
 		return "", err
 	}
 
-	var rspData map[string]interface{}
+	var rspData map[string]any
 	err = json.Unmarshal(buffer.Bytes(), &rspData)
 	if err != nil {
 		return "", err
@@ -102,7 +100,7 @@ func (s *WarpService) RegWarp(secretKey string, publicKey string) (string, error
 
 	deviceId := rspData["id"].(string)
 	token := rspData["token"].(string)
-	license, ok := rspData["account"].(map[string]interface{})["license"].(string)
+	license, ok := rspData["account"].(map[string]any)["license"].(string)
 	if !ok {
 		logger.Debug("Error accessing license value.")
 		return "", err
@@ -144,21 +142,20 @@ func (s *WarpService) SetWarpLicense(license string) (string, error) {
 		return "", err
 	}
 	defer resp.Body.Close()
-	buffer := bytes.NewBuffer(make([]byte, 8192))
-	buffer.Reset()
+	buffer := &bytes.Buffer{}
 	_, err = buffer.ReadFrom(resp.Body)
 	if err != nil {
 		return "", err
 	}
 
-	var response map[string]interface{}
+	var response map[string]any
 	err = json.Unmarshal(buffer.Bytes(), &response)
 	if err != nil {
 		return "", err
 	}
 	if response["success"] == false {
-		errorArr, _ := response["errors"].([]interface{})
-		errorObj := errorArr[0].(map[string]interface{})
+		errorArr, _ := response["errors"].([]any)
+		errorObj := errorArr[0].(map[string]any)
 		return "", common.NewError(errorObj["code"], errorObj["message"])
 	}