Skip to content

Commit

Permalink
携带部分param参数调用存在JSON反序列化错误
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelyPoker committed May 25, 2022
1 parent 5c32132 commit 1ead6d2
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ print(res.text)

1.修复ResultSet函数,在并发处理环境下存在数据丢失,响应延迟等问题。

2.handlerRequest携带部分param参数调用存在JSON反序列化错误,可以使用JsEnv_Dev.js去处理

## 其他案例

1. JsRpc实战-猿人学-反混淆刷题平台第20题(wasm)
Expand Down
126 changes: 126 additions & 0 deletions resouces/JsEnv_Dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
function Hlclient(wsURL) {
this.wsURL = wsURL;
this.handlers = {};
this.socket = {};
if (!wsURL) {
throw new Error('wsURL can not be empty!!')
}
this.connect()
this.handlers["_execjs"]=function (resolve,param){
var res=eval(param)
if (!res){
resolve("没有返回值")
}else{
resolve(res)
}

}
}

Hlclient.prototype.connect = function () {
console.log('begin of connect to wsURL: ' + this.wsURL);
var _this = this;
try {
this.socket["ySocket"] = new WebSocket(this.wsURL);
this.socket["ySocket"].onmessage = function (e) {
try{
let blob=e.data
blob.text().then(data =>{
_this.handlerRequest(data);
})
}catch{
console.log("not blob")
_this.handlerRequest(blob)
}

}
} catch (e) {
console.log("connection failed,reconnect after 10s");
setTimeout(function () {
_this.connect()
}, 10000)
}
this.socket["ySocket"].onclose = function () {
console.log("connection failed,reconnect after 10s");
setTimeout(function () {
_this.connect()
}, 10000)
}

};
Hlclient.prototype.send = function (msg) {
this.socket["ySocket"].send(msg)
}

Hlclient.prototype.regAction = function (func_name, func) {
if (typeof func_name !== 'string') {
throw new Error("an func_name must be string");
}
if (typeof func !== 'function') {
throw new Error("must be function");
}
console.log("register func_name: " + func_name);
this.handlers[func_name] = func;
return true

}

//收到消息后这里处理,
Hlclient.prototype.handlerRequest = function (requestJson) {
var _this = this;
try {
var result=JSON.parse(requestJson)
} catch (error) {
console.log("catch error",requestJson);
result = transjson(requestJson)
}
//console.log(result)
if (!result['action']) {
this.sendResult('','need request param {action}');
return
}
var action=result["action"]
var theHandler = this.handlers[action];
if (!theHandler){
this.sendResult(action,'action not found');
return
}
try {
if (!result["param"]){
theHandler(function (response) {
_this.sendResult(action, response);
})
}else{
var param=result["param"]
try {
param=JSON.parse(param)
}catch (e){
console.log("")
}
theHandler(function (response) {
_this.sendResult(action, response);
},param)
}

} catch (e) {
console.log("error: " + e);
_this.sendResult(action+e);
}
}

Hlclient.prototype.sendResult = function (action, e) {
this.send(action + atob("aGxeX14") + e);
}


function transjson(formdata){
var regex = /"action":(?<actionName>.*?),/g
var actionName = regex.exec(formdata).groups.actionName
stringfystring = formdata.match(/{..data..:.*..\w+..:\s...*?..}/g).pop()
stringfystring= stringfystring.replace(/\\"/g,'"')
paramstring = JSON.parse(stringfystring)
tens = `{"action":`+ actionName + `,"param":{}}`
tjson = JSON.parse(tens)
tjson.param = paramstring
return tjson
}

0 comments on commit 1ead6d2

Please sign in to comment.