-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsekiroFridaMini.js
1 lines (1 loc) · 6.51 KB
/
sekiroFridaMini.js
1
var SekiroClient=(function(){function SekiroClient(a){this.handlers={};this.isConnecting=false;this.sekiroOption=a;a.serverHost=a.serverHost||"sekiro.iinti.cn";a.serverPort=a.serverPort||5612;this.fridaSocketConfig={family:"ipv4",host:a.serverHost,port:a.serverPort};console.log(" welcome to use sekiro framework,\n"+" for more support please visit our website: https://iinti.cn\n");this.doConnect()}SekiroClient.prototype.registerAction=function(a,b){this.handlers[a]=b};SekiroClient.prototype.reConnect=function(){var a=this;console.log("sekiro try connection after 5s");setTimeout(function(){return a.doConnect()},5000)};SekiroClient.prototype.doConnect=function(){var b=this;if(this.isConnecting){return}this.isConnecting=true;console.log("sekiro connect to server-> "+this.fridaSocketConfig.host+":"+this.fridaSocketConfig.port);Socket.connect(this.fridaSocketConfig).then(function(a){b.isConnecting=false;a.setNoDelay(true);b.connRead(a);b.connWrite(a,{type:0x10,serialNumber:-1,headers:{'SEKIRO_GROUP':b.sekiroOption.sekiroGroup,'SEKIRO_CLIENT_ID':b.sekiroOption.clientId}})})["catch"](function(a){b.isConnecting=false;console.log("sekiro connect failed",a);b.reConnect()})};SekiroClient.prototype.connWrite=function(b,c){var d=this;b.output.write(this.encodeSekiroPacket(c))["catch"](function(a){console.log("sekiro write register cmd failed",a);d.reConnect()})};SekiroClient.prototype.connRead=function(b){var c=this;b.input.read(1024).then(function(a){if(a.byteLength<=0){b.close()["finally"](function(){console.log("sekiro server lost!");c.reConnect()});return}c.onServerData(b,a);setImmediate(function(){c.connRead(b)})})["catch"](function(a){console.log("sekiro read_loop error",a);c.reConnect()})};SekiroClient.prototype.onServerData=function(a,b){var c=this;if(!this.readBuffer){if(!b){return}this.readBuffer=b}else if(!!b){var d=new ArrayBuffer(this.readBuffer.byteLength+b.byteLength);var e=new Uint8Array(d);e.set(new Uint8Array(this.readBuffer),0);e.set(new Uint8Array(b),this.readBuffer.byteLength);this.readBuffer=d}var f=this.decodeSekiroPacket(a);if(!!f){this.handleServerPkg(a,f);setImmediate(function(){return c.onServerData(a)})}};SekiroClient.prototype.encodeSekiroFastJSON=function(a){var b=undefined;if(a.msg){b=this.str2Uint8(a.msg)}var c=undefined;if(a.data){c=this.str2Uint8(JSON.stringify(a.data))}var d=4+4+(b?b.length:0)+4+(c?c.length:0);var e=new ArrayBuffer(d);var v=new DataView(e);v.setInt32(0,a.status);v.setInt32(4,b?b.length:0);var f=8;if(b){new Uint8Array(e,8).set(b);f+=b.length}v.setInt32(f,c?c.length:0);f+=4;if(c){new Uint8Array(e,f).set(c)}return e};SekiroClient.prototype.handleServerPkg=function(b,c){if(c.type==0x00){this.connWrite(b,c);return}if(c.type!=0x20){console.log("unknown server message:"+JSON.stringify(c));return}var d=this;var f=function(a){setImmediate(function(){d.connWrite(b,{type:0x11,serialNumber:c.serialNumber,headers:{"PAYLOAD_CONTENT_TYPE":"CONTENT_TYPE_SEKIRO_FAST_JSON"},data:d.encodeSekiroFastJSON(a)})})};var g=function(a){f({status:0,data:a})};var h=function(a){f({status:-1,msg:a})};if(!c.data){h("sekiro system error, no request payload present!!");return}var i=this.uint8toStr(new Uint8Array(c.data));console.log("sekiro receive request: "+i);var j=JSON.parse(i);if(!j['action']){h("the param: {action} not presented!!");return}var k=this.handlers[j['action']];if(!k){h("sekiro no handler for this action");return}try{k(j,g,h)}catch(e){h("sekiro handler error:"+e+JSON.stringify(e))}};SekiroClient.prototype.decodeSekiroPacket=function(a){if(!this.readBuffer){return undefined}var v=new DataView(this.readBuffer);var b=v.getInt32(0);var c=v.getInt32(4);if(b!=0x73656b69||c!=0x726f3031){console.log("sekiro packet data");a.close().then(function(){console.log("sekiro close broken pipe")});this.readBuffer=undefined;return}var d=v.getInt32(8);if(this.readBuffer.byteLength<d+12){return}var e=v.getInt8(12);var f=v.getInt32(13);var g=v.getInt8(17);var h=18;var j={};for(var i=0;i<g;i++){var k=v.getInt8(h++);var l=this.uint8toStr(new Uint8Array(this.readBuffer.slice(h,k)));h+=k;var m=v.getInt8(h++);var n="";if(m>0){n=this.uint8toStr(new Uint8Array(this.readBuffer.slice(h,m)));h+=m}j[l]=n}var o=undefined;var p=(d+12-h);if(p>0){o=this.readBuffer.slice(h,h+p)}if(this.readBuffer.byteLength==d+12){this.readBuffer=undefined}else{this.readBuffer=this.readBuffer.slice(h)}return{type:e,serialNumber:f,headers:j,data:o}};SekiroClient.prototype.encodeSekiroPacket=function(c){var d=6;var e=[];for(var h in c.headers){e.push(this.str2Uint8(h));e.push(this.str2Uint8(c.headers[h]));d+=2}d+=e.reduce(function(a,b){return a+b.length},0);if(c.data){d+=c.data.byteLength}var f=new ArrayBuffer(d+12);var g=new DataView(f);g.setUint32(0,0x73656b69);g.setUint32(4,0x726f3031);g.setInt32(8,d);g.setInt8(12,c.type);g.setInt32(13,c.serialNumber);g.setInt8(17,Object.keys(c.headers).length);var i=18;e.forEach(function(a){g.setInt8(i++,a.length);new Uint8Array(f,i).set(a);i+=a.length});if(c.data){new Uint8Array(f,i).set(new Uint8Array(c.data))}return f};SekiroClient.prototype.uint8toStr=function(h){var i;var j;var k=0;var l=Math.min(256*256,h.length+1);var m=new Uint16Array(l);var n=[];var o=0;var p=function(){var b=k<h.length;if(!b||(o>=l-1)){var c=m.subarray(0,o);var d=[];c.forEach(function(a){return d.push(a)});n.push(String.fromCharCode.apply(null,d));if(!b){return{value:n.join('')}}h=h.subarray(k);k=0;o=0}var e=h[k++];if((e&0x80)===0){m[o++]=e}else if((e&0xe0)===0xc0){j=h[k++]&0x3f;m[o++]=((e&0x1f)<<6)|j}else if((e&0xf0)===0xe0){j=h[k++]&0x3f;i=h[k++]&0x3f;m[o++]=((e&0x1f)<<12)|(j<<6)|i}else if((e&0xf8)===0xf0){j=h[k++]&0x3f;i=h[k++]&0x3f;var f=h[k++]&0x3f;var g=((e&0x07)<<0x12)|(j<<0x0c)|(i<<0x06)|f;if(g>0xffff){g-=0x10000;m[o++]=(g>>>10)&0x3ff|0xd800;g=0xdc00|g&0x3ff}m[o++]=g}else{}};for(;;){var q=p();if(typeof q==="object")return q.value}};SekiroClient.prototype.str2Uint8=function(a){var b=0;var c=a.length;var d=0;var e=Math.max(32,c+(c>>>1)+7);var f=new Uint8Array((e>>>3)<<3);while(b<c){var g=a.charCodeAt(b++);if(g>=0xd800&&g<=0xdbff){if(b<c){var h=a.charCodeAt(b);if((h&0xfc00)===0xdc00){++b;g=((g&0x3ff)<<10)+(h&0x3ff)+0x10000}}if(g>=0xd800&&g<=0xdbff){continue}}if(d+4>f.length){e+=8;e*=(1.0+(b/a.length)*2);e=(e>>>3)<<3;var i=new Uint8Array(e);i.set(f);f=i}if((g&0xffffff80)===0){f[d++]=g;continue}else if((g&0xfffff800)===0){f[d++]=((g>>>6)&0x1f)|0xc0}else if((g&0xffff0000)===0){f[d++]=((g>>>12)&0x0f)|0xe0;f[d++]=((g>>>6)&0x3f)|0x80}else if((g&0xffe00000)===0){f[d++]=((g>>>18)&0x07)|0xf0;f[d++]=((g>>>12)&0x3f)|0x80;f[d++]=((g>>>6)&0x3f)|0x80}else{continue}f[d++]=(g&0x3f)|0x80}return f.slice?f.slice(0,d):f.subarray(0,d)};return SekiroClient}());