From ee05fca4460e702b5652fc3a0dfa149774300346 Mon Sep 17 00:00:00 2001 From: hliang <1820862012@qq.com> Date: Tue, 7 Jan 2025 22:34:38 +0800 Subject: [PATCH] update --- core/api.go | 72 +++++++++++++++++-------------------------- resouces/JsEnv_Dev.js | 23 ++++++-------- 2 files changed, 38 insertions(+), 57 deletions(-) diff --git a/core/api.go b/core/api.go index b22c22a..ff81a2b 100644 --- a/core/api.go +++ b/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) diff --git a/resouces/JsEnv_Dev.js b/resouces/JsEnv_Dev.js index 437daba..70c56c3 100644 --- a/resouces/JsEnv_Dev.js +++ b/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})); -} - +} \ No newline at end of file