hliang 4 месяцев назад
Родитель
Сommit
ee05fca446
2 измененных файлов с 38 добавлено и 57 удалено
  1. 28 44
      core/api.go
  2. 10 13
      resouces/JsEnv_Dev.js

+ 28 - 44
core/api.go

@@ -71,7 +71,7 @@ func ws(c *gin.Context) {
 	if group == "" {
 		return
 	}
-	//没有给客户端id的话 就用时间戳给他生成一个
+	//没有给客户端id的话 就用uuid给他生成一个
 	if clientId == "" {
 		clientId = utils.GetUUID()
 	}
@@ -83,6 +83,11 @@ func ws(c *gin.Context) {
 	client := NewClient(group, clientId, wsClient)
 	hlSyncMap.Store(group+"->"+clientId, client)
 	utils.LogPrint("新上线group:" + group + ",clientId:->" + clientId)
+	clientNameJson := `{"registerId":"` + clientId + `"}`
+	err = wsClient.WriteMessage(1, []byte(clientNameJson))
+	if err != nil {
+		log.Warning("注册成功,但发送回执信息失败")
+	}
 	for {
 		//等待数据
 		_, message, err := wsClient.ReadMessage()
@@ -141,49 +146,40 @@ func wsTest(c *gin.Context) {
 	}(testClient)
 }
 
-func GetCookie(c *gin.Context) {
+func checkRequestParam(c *gin.Context) (*Clients, string) {
 	var RequestParam ApiParam
 	if err := c.ShouldBind(&RequestParam); err != nil {
-		GinJsonMsg(c, http.StatusBadRequest, err.Error())
-		return
+		return &Clients{}, err.Error()
 	}
 	group := c.Query("group")
 	if group == "" {
-		GinJsonMsg(c, http.StatusBadRequest, "需要传入group")
-		return
+		return &Clients{}, "需要传入group"
 	}
-
 	clientId := RequestParam.ClientId
 	client := getRandomClient(group, clientId)
 	if client == nil {
-		GinJsonMsg(c, http.StatusBadRequest, "没有找到对应的group或clientId,请通过list接口查看现有的注入")
-		return
+		return &Clients{}, "没有找到对应的group或clientId,请通过list接口查看现有的注入"
 	}
+	return client, ""
+}
 
+func GetCookie(c *gin.Context) {
+	client, errorStr := checkRequestParam(c)
+	if errorStr != "" {
+		GinJsonMsg(c, http.StatusBadRequest, errorStr)
+		return
+	}
 	c3 := make(chan string, 1)
 	go client.GQueryFunc("_execjs", utils.ConcatCode("document.cookie"), c3)
 	c.JSON(http.StatusOK, gin.H{"status": 200, "group": client.clientGroup, "clientId": client.clientId, "data": <-c3})
 }
 
 func GetHtml(c *gin.Context) {
-	var RequestParam ApiParam
-	if err := c.ShouldBind(&RequestParam); err != nil {
-		GinJsonMsg(c, http.StatusBadRequest, err.Error())
-		return
-	}
-	group := c.Query("group")
-	if group == "" {
-		GinJsonMsg(c, http.StatusBadRequest, "需要传入group")
-		return
-	}
-
-	clientId := RequestParam.ClientId
-	client := getRandomClient(group, clientId)
-	if client == nil {
-		GinJsonMsg(c, http.StatusBadRequest, "没有找到对应的group或clientId,请通过list接口查看现有的注入")
+	client, errorStr := checkRequestParam(c)
+	if errorStr != "" {
+		GinJsonMsg(c, http.StatusBadRequest, errorStr)
 		return
 	}
-
 	c3 := make(chan string, 1)
 	go client.GQueryFunc("_execjs", utils.ConcatCode("document.documentElement.outerHTML"), c3)
 	c.JSON(http.StatusOK, gin.H{"status": 200, "group": client.clientGroup, "clientId": client.clientId, "data": <-c3})
@@ -196,21 +192,14 @@ func getResult(c *gin.Context) {
 		GinJsonMsg(c, http.StatusBadRequest, err.Error())
 		return
 	}
-
-	group := RequestParam.GroupName
-	if group == "" {
-		GinJsonMsg(c, http.StatusBadRequest, "需要传入group")
-		return
-	}
 	action := RequestParam.Action
 	if action == "" {
 		GinJsonMsg(c, http.StatusOK, "请传入action来调用客户端方法")
 		return
 	}
-	clientId := RequestParam.ClientId
-	client := getRandomClient(group, clientId)
-	if client == nil {
-		GinJsonMsg(c, http.StatusBadRequest, "没有找到对应的group或clientId,请通过list接口查看现有的注入")
+	client, errorStr := checkRequestParam(c)
+	if errorStr != "" {
+		GinJsonMsg(c, http.StatusBadRequest, errorStr)
 		return
 	}
 	c2 := make(chan string, 1)
@@ -228,20 +217,15 @@ func execjs(c *gin.Context) {
 	}
 	Action := "_execjs"
 	//获取参数
-	group := RequestParam.GroupName
-	if group == "" {
-		GinJsonMsg(c, http.StatusBadRequest, "需要传入group")
-		return
-	}
+
 	JsCode := RequestParam.Code
 	if JsCode == "" {
 		GinJsonMsg(c, http.StatusBadRequest, "请传入代码")
 		return
 	}
-	clientId := RequestParam.ClientId
-	client := getRandomClient(group, clientId)
-	if client == nil {
-		GinJsonMsg(c, http.StatusBadRequest, "没有找到对应的group或clientId,请通过list接口查看现有的注入")
+	client, errorStr := checkRequestParam(c)
+	if errorStr != "" {
+		GinJsonMsg(c, http.StatusBadRequest, errorStr)
 		return
 	}
 	c2 := make(chan string)

+ 10 - 13
resouces/JsEnv_Dev.js

@@ -1,4 +1,4 @@
-function Hlclient(wsURL) {
+var rpc_client_id, Hlclient = function (wsURL) {
     this.wsURL = wsURL;
     this.handlers = {
         _execjs: function (resolve, param) {
@@ -8,7 +8,6 @@ function Hlclient(wsURL) {
             } else {
                 resolve(res)
             }
-
         }
     };
     this.socket = undefined;
@@ -17,8 +16,10 @@ function Hlclient(wsURL) {
     }
     this.connect()
 }
-
 Hlclient.prototype.connect = function () {
+    if (this.wsURL.indexOf("clientId=") === -1 && rpc_client_id) {
+        this.wsURL += "&clientId=" + rpc_client_id
+    }
     console.log('begin of connect to wsURL: ' + this.wsURL);
     var _this = this;
     try {
@@ -43,13 +44,11 @@ Hlclient.prototype.connect = function () {
     });
     this.socket.addEventListener('error', (event) => {
         console.error('rpc连接出错,请检查是否打开服务端:', event.error);
-    });
-
+    })
 };
 Hlclient.prototype.send = function (msg) {
     this.socket.send(msg)
 }
-
 Hlclient.prototype.regAction = function (func_name, func) {
     if (typeof func_name !== 'string') {
         throw new Error("an func_name must be string");
@@ -60,10 +59,7 @@ Hlclient.prototype.regAction = function (func_name, func) {
     console.log("register func_name: " + func_name);
     this.handlers[func_name] = func;
     return true
-
 }
-
-//收到消息后这里处理,
 Hlclient.prototype.handlerRequest = function (requestJson) {
     var _this = this;
     try {
@@ -72,6 +68,10 @@ Hlclient.prototype.handlerRequest = function (requestJson) {
         console.log("请求信息解析错误", requestJson);
         return
     }
+    if (result["registerId"]) {
+        rpc_client_id = result['registerId']
+        return
+    }
     if (!result['action'] || !result["message_id"]) {
         console.warn('没有方法或者消息id,不处理');
         return
@@ -97,13 +97,11 @@ Hlclient.prototype.handlerRequest = function (requestJson) {
         theHandler(function (response) {
             _this.sendResult(action, message_id, response);
         }, param)
-
     } catch (e) {
         console.log("error: " + e);
         _this.sendResult(action, message_id, e);
     }
 }
-
 Hlclient.prototype.sendResult = function (action, message_id, e) {
     if (typeof e === 'object' && e !== null) {
         try {
@@ -113,5 +111,4 @@ Hlclient.prototype.sendResult = function (action, message_id, e) {
         }
     }
     this.send(JSON.stringify({"action": action, "message_id": message_id, "response_data": e}));
-}
-
+}